Javascript · Variables
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Which keyword allows changes?
You need to create a variable for a Codémon's health that will decrease during battles. Which keyword should you use?
Options
• ```let```
• ```const```
• ```var```
• Any of them
Hint : Ask yourself whether this value will still be the same after a few battles.
Exercise 2 — Complete the string
Add the missing quotes to create a valid string:
let element = ____Water____; console.log(element);
Options: " " ' ` « »
Hint : Think of a string as something you open and then close. Look carefully at both ends of the text.
Exercise 3 — Valid camelCase name?
Which variable name follows camelCase convention?
Options
• ```attackPower```
• ```AttackPower```
• ```attack_power```
• ```ATTACKPOWER```
Hint : Reread the name of the convention itself: the way it is written shows you how it works.
Exercise 4 — Match values to types
Match each value with its correct type:
Items to match
• 100
• "100"
• true
• "true"
Definitions
• number
• string
• boolean
• string
Exercise 5 — Correct boolean syntax?
Which is the correct way to create a boolean variable?
Options
• ```let isActive = true;```
• ```let isActive = "true";```
• ```let isActive = True;```
• ```let isActive = TRUE;```
Hint : Two things separate these options: whether the value is treated as text, and how it is capitalised. Only one option gets both right.
Exercise 6 — Use typeof operator
Complete the code to check the type of the variable:
let score = 42; console.log(____ ____);
Options: typeof score typeOf type "score"
Hint : One of these options is the operator made for this job; the others are traps, so check the spelling and the capital letters. Then ask yourself whether you want to inspect the variable itself or a piece of text.
Exercise 7 — undefined vs null
What value does an unassigned variable have?
let codemon; console.log(codemon);
Options
• ```undefined```
• ```null```
• ```0```
• ```""```
Hint : The variable was declared but never given anything. Does JavaScript leave it empty on its own, or does someone have to put that emptiness there?
Exercise 8 — Declare a constant
Create a constant for the game version that never changes:
____ gameVersion ____ "1.0.0";
Options: const let var = == ===
Hint : A game version never moves once it is set: let that guide the first blank. For the second, ask yourself whether you are giving a value or comparing two of them.
Exercise 9 — Which is a number?
Which of these values is a number type?
Options
• ```3.14```
• ```"3.14"```
• ```'3.14'```
• ```3,14```
Hint : Ask yourself which of these values JavaScript could do maths with.
Exercise 10 — Match typeof results
Match each value with what
typeof
returns:
Items to match
• typeof "Hello"
• typeof 99
• typeof false
• typeof undefined
Definitions
• "string"
• "number"
• "boolean"
• "undefined"
Exercise 11 — Assign null value
Create a variable with
null
to represent an empty inventory slot:
let emptySlot ____ ____; console.log(emptySlot);
Options: = null undefined "null" NULL
Hint : The slot is empty on purpose, not by accident, and that difference matters here. Watch out too for options that only look right: capital letters and quotes change everything.
Exercise 12 — Invalid variable name?
Which variable name is NOT allowed in JavaScript?
Options
• ```player-score```
• ```playerScore```
• ```player_score```
• ```playerScore2```
Hint : Read each name the way JavaScript would: one of them contains a symbol that JavaScript already uses for something else.
Exercise 13 — Convert to camelCase
Convert these variable names to proper camelCase:
let ____ = 50; // max_health let ____ = true; // is_ready
Options: maxHealth isReady max_health MaxHealth is_ready IsReady
Hint : The comment on each line gives you the original name. Think back to how the naming chapter glued several words into a single name, and compare the options letter by letter.
Exercise 14 — Which quotes work?
Which of these creates a valid string in JavaScript?
Options
• All three types work: ```"text"```, ```'text'```, `` `text` ``
• Only double quotes work: ```"text"```
• Only single quotes work: ```'text'```
• Only backticks work: `` `text` ``
Hint : Look back at how strings were written in the primitive types chapter: was there only one accepted way, or several?
Exercise 15 — Match keyword to usage
Match each keyword with when to use it:
Items to match
• let
• const
• var
Definitions
• Value can change
• Value never changes
• Old way (avoid)
Exercise 16 — Create number variables
Create two number variables with the correct syntax.
wholeNumber
should be an integer and
decimal
should be a decimal number:
let wholeNumber = ____; let decimal = ____; console.log(wholeNumber, decimal);
Options: 10 5.5 "10" "5.5" ten five
Hint : One value has to be whole, the other has to have a decimal part. Then ask yourself which of the remaining options JavaScript would read as text instead of as a number.
Exercise 17 — What does typeof return?
What type of value does the
typeof
operator always return?
Options
• A string
• A number
• A boolean
• It depends on the input
Hint : Think back to what appeared in the console when you used this operator: was the result something you could calculate with, or something you could read?
Exercise 18 — Complete boolean variables
Fill in the boolean values. Game was won and not lost. You should know which value to use where.
let isWinner = ____; let isLoser = ____; console.log(isWinner, isLoser);
Options: true false "true" "false" TRUE FALSE
Hint : The sentence in the question tells you which state each variable is in. Then pick the writing that JavaScript understands as a real value and not as text.
Exercise 19 — How many primitive types?
How many primitive types have you learned so far?
Options
• 3 types (string, number, boolean)
• 2 types (string, number)
• 1 type (undefined)
• 5 types
Hint : Go back through the chapter on primitive types and count only the ones you actually practised writing yourself.
Exercise 20 — Match errors to problems
Match each code issue with its problem:
Items to match
• let name = Aquachu;
• let isActive = "true";
• let level = "50";
• const hp = 100; hp = 50;
Definitions
• Missing quotes for string
• Boolean has quotes
• Number has quotes
• Cannot reassign const
Exercise 21 — When to use null?
When should you use
null
instead of
undefined
?
Options
• When you want to intentionally represent "no value"
• When you forget to assign a value
• Never, always use undefined
• They are the same thing
Hint : One of these two values shows up on its own; the other only exists if you write it. Start from that difference.
Exercise 22 — Declare multiple variables
Complete this code to create three different types of variables:
____ playerName = ____Pierre____; ____ score = ____; ____ hasWon = ____; console.log(playerName, score, hasWon);
Options: let let let " " ' 100 "100" true "true"
Hint : Ask two questions on each line: will this value ever change later, and does JavaScript need a mark to know it is text?
Exercise 23 — What happens with const?
What happens if you try to change a
const
variable?
const maxPlayers = 4; maxPlayers = 6;
Options
• JavaScript throws an error
• The value changes to 6
• The value stays 4 silently
• It creates a new variable
Hint : Ask yourself why you would pick this keyword rather than another one. If changing the value were allowed, would that choice still mean anything?
Exercise 24 — Good vs bad names
Match each bad variable name with a better alternative:
Items to match
• player_name
• MaxHealth
• attack-power
• CODEMON_TYPE
Definitions
• playerName
• maxHealth
• attackPower
• codemonType
Exercise 25 — Review all concepts
Complete this code using everything you've learned:
____ gameName = "Codémon"; ____ currentLevel = ____; let isPlaying = ____; let nextCodemon; console.log(gameName, ____ gameName); console.log(currentLevel); console.log(isPlaying); console.log(nextCodemon);
Options: const let 15 "15" true typeof type
Hint : Take the blanks one at a time. For each declaration, ask whether that value will ever change during the game. For each value, ask whether JavaScript should read it as text. For the last blank, look at what that line is meant to display.
Back to the course