JavaScript Variables and Data Types Worksheet

Question 1

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

W3Schools
Question 2

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

False, they are case sensitive.
Question 3

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

JavaScript variables must be identified with unique names. When creating a unique name as a identifier we do have to avoid using reserved words or words that are JavaScript keywords. They are also case sensitive and can be short or more descriptive names.
Question 4

What is 'camelCase'?

It is a naming convention where the first letter of each word is capitalized, except for the first word.
Question 5

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

Some of the different Data Types are: Numbers: Represents both integer and floating-point numbers. String: A sequence of characters. Boolean: A logical value, either true or false. Null: The intentional absence of a value. Undefined: A variable that has been declared but not assigned a value. Symbol: A unique and immutable value. BigInt: An integer value of arbitrary precision. Object: A collection of key-value pairs.
Question 6

What is a boolean data type?

As mentioned above boolean data type is either true or false.
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;

JavaScript would see that as a declared variables that hasn't been created or a wrong variable and most likely will results in an error.
Question 8

What character is used to end a statement in JavaScript?

The semicolon 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?

It will be stored as 'Undefined' and this is usually used as a place holder.
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);
In the console log it would display 9888 string. This is because there are "" around one of the two variables but not the other. So it will attempt to add a number to a string and result in a new string; which is why we got the type back as string. If we wanted to have this display as attened we would remove the quotes from the second variable and it should display as 186 number in the console log.
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);
It will display a 'Total' then followed by the variable which in this case is 99.
Question 12

What is the difference between these two variables?


const score1 = 75;
const score2 = "75";
The first variable would be considered a number while the second variable 'score2' would be seen as a string data type.
Question 13

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


const score = 0;
score = prompt("Enter a score");
By using the const we are saying that this variable can't be changed. We then are asking for someone to 'enter a score' which in turn are trying to change the variable. If we wanted to correct this we would change the const vairable to a let and it should not crash the program as currently written.

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: