JavaScript Variables and Data Types Worksheet
Question 1Find a reference that lists all the keywords in the JavaScript programming language.
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
True 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?
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.
What is 'camelCase'?
Example of camelCase: myVariable, totalScore, userProfileName.
What are ALL the different data types in JavaScript (note that there are some that we did not discuss in class)?
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.
What is a boolean data type?
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.
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;
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.
What character is used to end a statement in JavaScript?
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.
If you declare a variable, but do not initialize it, what value will the variable store?
Example:
let age;
console.log(age); // Output is "undefined"
So, if no value is set, JavaScript just makes it `undefined`.
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);
**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.
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);
**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).
What is the difference between these two variables?
const score1 = 75;
const score2 = "75";
- **`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.
Explain why the this code will cause the program to crash:
const score = 0;
score = prompt("Enter a score");
- `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:
- 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!