These are the first “test yourself” questions here and my answers below.
- How do you declare a variable?
You write “let”, “const”, or “var” followed by the name of the variable you want to create.
- What are three different ways to declare a variable?
You can use “let”, “const”, or “var”.
- Which one should you use when?
“Let” is the currently accepted way to declare a new variable. You should use it for any new variable, except constant (unchanging) variables, these can be declared with “const”.
“Var” is the older way of declaring variables. There are reasons for not using it because of it’s behaviour, but it is still seen in older scripts.
- What are the rules for naming variables?
- Use letters, digits, $ or _
- First character cannot be a digit
- They are case sensitive – use camelCase
- Do not use non-English letters
- There are reserved words (e.g. let, class, return, function)
- Define the variable before using it
- Make names concise but descriptive and logical
- What are operators, operands, and operations?
Operators – they define the operation to be performed between two operands.
Operands – the things operated on (like numbers)
Operations – when we perform an action between two numbers or variables (like division, addition, incrementation)
- What is concatenation and what happens when you add numbers and strings together?
Concatenation is when two things get added together as a string. Like, “My” “3rd” “Dessert” when added would become “My3rdDesert”.
If you add numbers and strings together they will concatenate into a string. Like, “2” “Apples” would be “2Apples”.
/= Division assignment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
=== Strict Equality
!== Strict Non-Equality
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Rarely used bitwise operators:
<< LEFT SHIFT
>>> ZERO-FILL RIGHT SHIFT
- What is the difference between
== is equality.
=== is strict equality.
e.g. let x = “123”;
let y = new Number(123);
x == y is true.
x === y is not true.
Equality is just the same value. But strict equality needs to be the same value and the same type (x is a number, y is a number object).
- What are operator precedence values?
- What are the increment/decrement operators?
Increment and decrement take the value and then change it. Increment adds 1 to the value. Decrement minuses 1 from the value.
- What is the difference between prefixing and post-fixing them?
Prefix – returns new value:
If you put the increment/decrement operator first (prefix) it will perform the action of incrementing/decrementing and then returns the new value to be used right away.
Postfix – returns old value:
If you put the increment/decrement operator second (post-fix) it will return the old value first, then increment/decrement the value.
- What are assignment operators?
Assignment operators assign a value to a variable.
They are also able to change the value while assigning it.
n *= 3+5 is short for n = n * (3+5)
The assignment operators have the same precedence as a normal assignment operator, so they run after most other calculations are already done.
- What is the “Unary +” Operator?
Unary operator acts on a single operand to operate.
For example, x = +x;
If you had let a = 10 then console.log(-a) it would show “-10”.
Whereas, in a binary operation like x + y there are two operands. This means the “+” acts as addition and concatenates them.