JS Variables Flashcards
- Which of the following are correct variable declarations?
a. let name = “Max”;
b. let my name = “Jane”;
c. const age = 37;
d. const 1name = ‘Henry’;
a
c
- Why would you declare variables with const const instead of let?
You would use “const” to declare variables whose value is not going to change. On the other hand, you would use “let” to declare variables whose value you expect to change.
- What would the result of the code below be and why?
const name = “Paul”;
name = “Jeremy”;
Error - as it is not possible to assign a new value to a const variable.
- What would the result of the code below be and why?
let country = “Spain”;
const country = “Spain”;
SyntaxError - it is not possible to declare two variables with the same name.
- What would the result of the code below be and why?
const name;
name = “Paul”;
Error - a const variable must be initialized during the declaration
- Which variable names are valid from this list?
a. _favMovie
b. $sport
c. tv show
d. actorName
e. school name
f. 5days
a
b
d
Reminder : Variable Naming Rules
- Variable names must begin with a letter, a dollar sign $ , or an underscore _ .
- Variable names can contain letters (uppercase and lowercase), numbers and the symbols _ and $.
- Variable names must not contain spaces.
- Variable names are case sensitive (Name, name and NAME are all different variables)
- Reserved keywords cannot be used as variable names.
- Declare a variable for your favorite food and name it using camelCase
let favoriteFood = “Pizza”;
- The following code has some errors, what are they?
const myTown;
myTown = “London”;
myTown = “Madrid”;
When variables are declared using const they must be initialized with the value during the declaration, on the same line.
- The following code has some errors, what are they?
const myCountry = “Brazil”;
myCountry = “France”;
After a “const” variable is declared and initialized, its value cannot be changed by reassignment.
- What is the output of the following console.log?
let hello;
console.log(hello);
undefined
- What would the result of the code below be and why?
const super = 50;
Error because super is a reserved keyword.
- Which of the following are true?
a. Variable names are case sensitive
b. Variable names cannot start with numbers
c. Using camelCase notation is mandatory
d. Variables can start with $
a
b
d
Reminder : Variable Naming Rules
- Variable names must begin with a letter, a dollar sign $ , or an underscore _ .
- Variable names can contain letters (uppercase and lowercase), numbers and the symbols _ and $.
- Variable names must not contain spaces.
- Variable names are case sensitive (Name, name and NAME are all different variables)
- Reserved keywords cannot be used as variable names.
- What is the output of the following console.log?
let name = “John”;
console.log(“My name is “ + name);
My name is John
- Bonus Question: What is the output of the following console.log and why?
let b = 20;
b *= 2;
console.log(b);
- The operator *=, also known as the multiplication assignment, is used to perform a multiplication and assignment of the result at the same time. If written in the usually way using * and = separately, the expression b *= 2 is written as b = b * 2.
- Bonus Question: Would this code cause an error? Why/why not?
let y;
y = 9;
y = “Dog”;
No, because when a variable is declared using let its value can change.
How to create a variable?
The actions of creating a variable and giving it some value are known as variable declaration and initialization.
What is a variable declaration?
A variable declaration is when you create a variable and give it a name.
What is a variable initialization?
Variable initialization is when you assign some value to a newly declared variable.
What operator assignment do you used for a variable ?
Assignment operator “=”
The assignment operator = assigns a value specified on the right-hand side to a variable on the left-hand side.
We use the assignment operator = to assign (give) a value to an existing variable. Please do not confuse it with the mathematical equal symbol =, which is used to represent equality.
What is happening if you don’t assign a value to a declared variable ?
Suppose you declare a variable, but you don’t assign it a value. In that case, its default value will be initialized to undefined
When do you use let or const and why ?
let is used to declare variables whose value may change in the future.
const is used to declare a variable that will remain constant and whose value can’t be reassigned.
What are the variable naming rules ?
Reminder : Variable Naming Rules
- Variable names must begin with a letter, a dollar sign $ , or an underscore _ .
- Variable names can contain letters (uppercase and lowercase), numbers and the symbols _ and $.
- Variable names must not contain spaces.
- Variable names are case sensitive (Name, name and NAME are all different variables)
- Reserved keywords cannot be used as variable names.
Example for reserved keywords
We said we could use any English or non-English word as a variable name. However, JavaScript has certain reserved keywords that cannot be used as variable names. For example, the words let, class, return and function are not allowed as variable names.
break
case
catch
class
const
continue
debugger
default
delete
do
else
export
extends
false
finally
for
function
if
import
in
instanceof
new
null
return
super
switch
this
throw
true
try
typeof
var
void
while
with
What are the two categories for Data Types in JavaScript
Primitive data types (string, number, boolean, undefined, null, bigInt_ and symbol_)
Non-Primitive data types (object, array)