JavaScript Variables and Data Types Worksheet

Question 1

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

Control Flow:

if, else, switch, case, default try, catch, finally, throw break, continue
Declarations & Functions: var, let, const function, return class, extends, constructor, super
Loops: for, while, do in (used in for...in), of (used in for...of)
Data Types & Literals: null, undefined true, false this, new, typeof, instanceof, void, delete
Modules (ES6+): import, export, default, as

Question 2

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

False
Question 3

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

Start with Letter, Underscore, or Dollar Sign: First letter gotta be a letter (a-z, A-Z), _ (underscore), or $ (dollar sign). Use Letters, Numbers, Underscore, Dollar Sign:
After first letter, can use letters, numbers (0-9), _, or $.
Uppercase and Lowercase Matter: myVar and MyVar not same thing; they’re different. No Reserved Words:
Don’t use words JavaScript already took, like let, for, class.
Question 4

What is 'camelCase'?

In programming, camelCase is a naming style where each new word in the name starts with a capital letter (except the first word). It looks like the humps on a camel, hence the name!

Example of camelCase: myVariable, totalScore, userProfileName.
Question 5

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

1. String: For words or sentences, any text stuff.
2. Number: For all kinds of numbers, like whole ones or with decimals.
3. BigInt: For really huge numbers, bigger than regular numbers.
4. Boolean: For true or false, like yes or no.
5. Symbol: Special unique things, often for keys in objects.
6. Undefined: Means the thing exists but no value in it yet.
7. Null: Means empty on purpose, set by programmer.
8. Object: Used for groups of data, like name and age together. Has more types like:
- Array: Just a list of items, one after another.
- Function: Some code that does something when called.
- Date: For dates and times stuff.
- RegExp: Patterns to match text, like for searching.
- Map: Group with items, each has a key and value.
- Set: Just a group of unique things, no repeats.
Question 6

What is a boolean data type?

A boolean data type only has two values: true or false.
It’s used to check if something is or isn’t, like:

- Is the user logged in? true or false
- Did the button get clicked? true or false

Booleans help decide what code should run based on if something’s 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;

If you forget quotes around a string, like this:

let lastName = Jones;

JavaScript thinks Jones is a variable name, not a string.
If there’s no variable named Jones, JavaScript gives an error (ReferenceError).

To make it a string, use quotes like this:

let lastName = "Jones";

Now JavaScript knows it’s a string and it works fine.
Question 8

What character is used to end a statement in JavaScript?

In JavaScript, a **semicolon** (`;`) ends a statement. It tells JavaScript the line is done.

Example:
let name = "Alice";

JavaScript doesn’t always need semicolons (it sometimes adds them itself), but it’s a good habit to use them so there’s no weird errors.
Question 9

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

If you make a variable but don’t give it a value, the variable will have **`undefined`**.

Example:
let age;
console.log(age); // Output is "undefined"

So, if no value is set, JavaScript just makes it `undefined`.
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 program will produce the following output in the console log:

**Output:**
988
string

**Explanation:**
- `firstTestScore` is a number (98), and `secondTestScore` is a string ("88").
- When you use `+` with a number and a string, JavaScript treats the number as a string too and **joins** them together (this is called "concatenation").
- So, `sum` becomes `"988"`, not the number `186`.
- `console.log(sum);` shows `988` in the console.
- `console.log(typeof sum);` shows `"string"` because `sum` is now treated as a string.
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 program will show this in the console:

**Output:**
total
99

**Explanation:**
- `console.log("total");` prints the word `"total"` because it’s in quotes.
- `console.log(total);` prints the value of `total`, which is `99`.
- So, first you see `total` (just text), then you see `99` (the value).
Question 12

What is the difference between these two variables?


const score1 = 75;
const score2 = "75";
The difference between these two variables is:

- **`score1`** is a **number** with value `75`.
- **`score2`** is a **string** with value `"75"` (it’s got quotes, so it’s just text, not a number).

So, `score1` can do math stuff, but `score2` is only text and won’t work the same in math.
Question 13

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


const score = 0;
score = prompt("Enter a score");
This code will crash because:

- `const score = 0;` makes `score` a **constant**. Constants can’t change once set.
- `score = prompt("Enter a score");` tries to change `score` to what user enters, but that’s not allowed for constants.

So, the program crashes because it’s trying to change a constant, and JavaScript doesn’t like that.

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: