JavaScript Variables and Data Types Worksheet

Question 1

Find a reference that lists all the keywords in the JavaScript programming language.

JavaScript Keywords
Question 2

True or false: keywords and variable names are NOT case sensitive.

This is false. Keywords and variable names ARE case sensitive.
Question 3

There are some rules for how you can name variables in JavaScript. What are they?

There are a few rules to be followed when naming variables:
Question 4

What is 'camelCase'?

camelCase is the naming convention used in JavaScript. Since names can't contain spaces, this is a different approach to creating a name. Beginning a name with a lowercase letter is required, and if a new word is added, that word will start with a capital letter.

For Example: threeWordVariable, firstName, or daysUntilChristmas
Question 5

What are ALL the different data types in JavaScript (note that there are some that we did not discuss in class)?

There are 8 data types in JavaScript:
  1. Number
  2. String
  3. Boolean
  4. Bigint
  5. Object
  6. Symbol
  7. Null
  8. Undefined
The data types currently being taught in the Web Programming 1 course are number, string, and boolean.
Question 6

What is a boolean data type?

A boolean data type is a value that can either be true or false. This is used often in programming as programmers want the code to do one thing if something is true, and another thing if something is false.

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.
Question 7

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;

When viewing the console tab of the Web Developer Tools (F12), an error will show as follows:

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.
Question 8

What character is used to end a statement in JavaScript?

The semicolon (;) character is used to end a statement in JavaScript.
Question 9

If you declare a variable, but do not initialize it, what value will the variable store?

The variable will not show a value, as it will show as undefined in the console log.

For Example:
Question 10

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);
The console log will show a sum of 9888 and typeof sum (the sum's data type) of string.

If a number and a string data type are added together to form the expression sum: This is known as string concatenation. This is usually described as adding strings together, but since one of the variables of the expression above was a string, it results in a string data type.
Question 11

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);
The console log will show the word total and the number 99.
Question 12

What is the difference between these two variables?


const score1 = 75;
const score2 = "75";
These are two different data types. The difference is 75 is a number, while "75" is a string.
Question 13

Explain why the this code will cause the program to crash:


const score = 0;
score = prompt("Enter a score");
Both of the lines above are trying to declare the same variable: score. Since score was declared and initialized as a constant variable, that means if there is an attempt to change the variable after the declaration/initialization has been made (like a number being typed into the prompt on the web page), it will cause an error to occur. In the console log, the error would show as follows:

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: