JavaScript Variables and Data Types Worksheet
Question 1Find a reference that lists all the keywords in the JavaScript programming language.
Question 2True or false: keywords and variable names are NOT case sensitive.
False: Keywords and variable names are case sensitive. For example, Var and var would be considered different identifiers in most programming languages, including JavaScript.
There are some rules for how you can name variables in JavaScript. What are they?
In JavaScript, variable names are case-sensitive and must start with a letter, underscore (_), or dollar sign ($). They can't begin with a number. Subsequent characters can be letters, digits, underscores, or dollar signs. Reserved keywords like let and const can't be used as names. Following these rules helps keep your code clear and error-free.
What is 'camelCase'?
'camelCase' is a way of writing compound words or phrases where each word, except the first, starts with a capital letter, and no spaces or underscores are used. For example, myVariableName and firstName. It's commonly used in programming for naming variables and functions because it's easier to read and understand.
What are ALL the different data types in JavaScript (note that there are some that we did not discuss in class)?
JavaScript has several data types, including primitive types like strings, numbers, booleans, undefined, null, symbols, and BigInt. Additionally, it includes complex types like objects, arrays, functions, dates, and regular expressions. Each of these types serves different purposes, allowing JavaScript to handle a wide range of applications efficiently.
What is a boolean data type?
A boolean data type represents a value that can be either true or false. It's often used in programming to make decisions and control the flow of logic, such as in conditional statements and loops. Think of it as a simple on/off switch for various checks and comparisons in your code.
What happens if you forget to put quotes around a string when you initialize a variable to a string value?
How does JavaScript try to interpret this?
For example: var lastName = Jones;
If you forget to put quotes around a string when initializing a variable to a string value in JavaScript, such as var lastName = Jones;, JavaScript will interpret Jones as an undefined variable or identifier rather than a string. This will result in a ReferenceError because JavaScript will try to find a variable named Jones which hasn't been declared.
What character is used to end a statement in JavaScript?
n JavaScript, a statement is typically ended with a semicolon (;). While JavaScript can often infer where statements end, explicitly using semicolons helps ensure your code runs as intended and avoids potential errors.
If you declare a variable, but do not initialize it, what value will the variable store?
If you declare a variable in JavaScript but do not initialize it, the variable will store the value undefined. This indicates that the variable has been declared but has not yet been assigned a value.
What output will the following program produce? In other words, explain what you would see in the console log if you ran a program like this:
const firstTestScore = 98;
const secondTestScore = "88";
const sum = firstTestScore + secondTestScore;
console.log(sum);
console.log(typeof sum);
When you run the program, you'll see "9888" and "string" in the console. This happens because the number 98 is added to the string "88", and JavaScript treats them both as strings and sticks them together, making "9888". The console.log(sum); shows this result, and console.log(typeof sum); tells you that sum is a string. This is an example of how JavaScript combines different types of data.
What output will the following program produce? In other words, explain what you would see in the console log if you ran a program like this:
const total = 99;
console.log("total");
console.log(total);
When you run the program, you'll see "total" and "99" in the console. The console.log("total"); line prints the word "total" as a string. Then, console.log(total); prints the value of the total variable, which is 99. So, you first see the word "total" and then the number 99 on the next line.
What is the difference between these two variables?
const score1 = 75;
const score2 = "75";
The difference between score1 and score2 is in their types. score1 is a number (75), used for math operations and comparisons. On the other hand, score2 is a string ("75"), which is treated as text. Even though they look the same, score1 and score2 behave differently because one is a number and the other is a string
Explain why the this code will cause the program to crash:
const score = 0;
score = prompt("Enter a score");
This code will make the program crash because score is declared with const, which means it can't be changed once it's set. When the program tries to change score with the value from prompt("Enter a score");, it throws an error since const variables can't be reassigned. To fix this, you can use let instead of const, which allows score to be changed.
Coding Problems
Coding Problems - See the 'script' element below this h1 element. You will have to write some JavaScript code in it.
Here are some tips to help you with the coding problems:
- To do this assignment, you'll need to know how to open the brower's developer tools (press F12) and look at the console log.
- Pay extremely close attention to syntax! One little syntax error can mess up your entire program.
- After you complete each problem, refresh the page in the browser and look in the console your log message and for errors.
- If you have an error in the console, check the line number of the error then look for syntax errors on that line (and the line before and the line after that line).
- If you get stuck on this assignment, please ask for help! Dealing with syntax errors can be extremely frustrating for beginners. It takes a lot of effort and support to get comfortable with the syntax of a language. Don't be afraid to ask for help, your success in this class will depend on your willingness to get help!