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.
There are some rules for how you can name variables in JavaScript. What are they?
- Begin the variable with a lowercase letter
- Do not begin with a space
- Do not begin with a number
- Using either a space or number at the beginning of the variable will cause an error
- Avoid using spaces altogether when naming a variable, as that will also cause an error
- camelCase
- Begin the variable with a lowercase letter - for each new word used in the variable, use a capital letter
- This helps determine the separate words included in the variable, as using spaces will cause an error
- Begin the variable with a lowercase letter - for each new word used in the variable, use a capital letter
- When creating your naming convention, be sure to adhere to the rules above, as well as stay consistent, be aware that JavaScript is case sensitive, and use names to effectively describe the data that the variable is storing
What is 'camelCase'?
For Example: threeWordVariable, firstName, or daysUntilChristmas
What are ALL the different data types in JavaScript (note that there are some that we did not discuss in class)?
- Number
- String
- Boolean
- Bigint
- Object
- Symbol
- Null
- Undefined
What is a boolean data type?
The keywords true and false in JavaScript are used to assign values to variables that are meant to hold booleans.
For Example: let done = false; //the data type is boolean
If the keyword is placed inside quotes (let x = "false";), this changes the data type to string. When leaving the quotations off (let x = false;), the data type would be boolean.
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;
Uncaught ReferenceError: Jones is not defined
The JavaScript interpreter will try to interpret Jones as a variable, but Jones has not yet been defined in the code, which causes an error to occur. To initialize a variable with a string value, you must enclose the string in either single or double quotes.
When written correctly (var lastName = "Jones";), the data showing in the console log will show just the text Jones.
What character is used to end a statement in JavaScript?
If you declare a variable, but do not initialize it, what value will the variable store?
For Example:
- Declaring: let firstNumber; - the value will be stored as undefined
- Initializing: let firstNumber = 1; - the value will be stored as 1
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);
If a number and a string data type are added together to form the expression sum:
- the sum will attach the string to the number being added to it
- the resulting data type will always be a string
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);
What is the difference between these two variables?
const score1 = 75;
const score2 = "75";
Explain why the this code will cause the program to crash:
const score = 0;
score = prompt("Enter a score");
Uncaught TypeError: Assignment to constant variable.
If the declaration initialized the variable using the keyword let, this would allow a change to be made to the variable, even after it has been initialized. If the code is instead written as let score = 0; along with score = prompt("Enter a score");, then the original variable is able to be changed from the value 0 without an error occuring. The number the user enters into the prompt will be the number that is shown in the console log.
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!