JavaScript Fundamentals Flashcards
Can you overwrite variable declarations without an error using var?
Yes; var camper = 'James'; var camper = 'David'; console.log(camper); // logs 'David'
What are the six falsy values?
undefined, null, NaN, 0, “” (empty string), and false
Will these return true or false?
1) false == “”
2) NaN == undefined
3) 0 == null
4) null == undefined
5) NaN == NaN
6) 0 == “”
1) false == “”, true
2) NaN == undefined, false
3) 0 == null, false
4) null == undefined, true
5) NaN == NaN, false (NaN is not equivalent to anything, not even itself!)
6) 0 == “”, true
If can use Object.is(NaN, NaN) to see if it is equal (will return true)
What are the 7 primitive data types of JavaScript?
undefined Boolean Number String BigInt Symbol Null
Why is Math.max() smaller than Math.min()?
If no arguments are given, Math.min() returns infinity and Math.max() returns -infinity . This is simply part of the specification for the max() and min() methods, but there is good logic behind the choice.
What are the differences between var, let, and const?
Const can't be reassigned (although does allow for variable mutation... just can't re-assign the variable itself). Const also can't be declared without a value... will have syntax error All declarations (function, var, let, const and class) are hoisted in JavaScript, while the var declarations are initialized with undefined, but let and const declarations remain uninitialized. During compile time, JavaScript only stores function and variable declarations in the memory, not their assignments (value). They will only get initialized when their lexical binding (assignment) is evaluated during runtime by the JavaScript engine. Var is function scoped while let and const are block scoped (scoped within things like if statements and for loops
What are things not allowed in ‘use strict’
Strict mode changes previously accepted “bad syntax” into real errors.
- Using a variable, without declaring it, is not allowed like x=5
- Mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.
- assigning values to non-writable properties
- deleting a variable or function
- duplicating parameters
- The this keyword refers to the object that called the function. If the object is not specified, functions in strict mode will return undefined and functions in normal mode will return the global object (window):
What happens if you assign a variable without a keyword?
If not using strict mode, it will be assigned to the window. So x = 1 will be window.x = 1
What is a ReferenceError and name common examples.
A ReferenceError indicates that an invalid reference value has been detected.
A JavaScript program is trying to read a variable that does not exist:
dog
dog = 2 (need use strict)
Or there are unexpected assignments (such as using = when you should use ==
What is a SyntaxError and name some examples.
- A function statement without name
- Missing comma after an object property definition
- Trying to use a reserved identifier like delete or class
- Missing ) or } or ; or ,
What is a type error and name some examples.
A TypeError happens when a value has a type that’s different than the one expected
- Mistyping a method… blank is not a function
- Unexpected undefined and null types
What does this return?
alert(“1” + 2 + 2);
“122”
Here, the first operand is a string, the compiler treats the other two operands as strings too. The 2 gets concatenated to ‘1’, so it’s like ‘1’ + 2 = “12” and “12” + 2 = “122”.
What does this return?
alert(2 + 2+ “1”);
“41”
Here, operators work one after another. The first + sums two numbers, so it returns 4, then the next + adds the string 1 to it, so it’s like 4 + ‘1’ = ‘41’.
let counter = 1 let a = ++counter; **************** let counter = 1 let a = counter++;
What is the value of a in each situation and why?
In the first scenario, ‘a’ returns 2 because the prefix form returns the new variable.
In the second scenario, ‘a’ returns 1 because the postfix form returns the old variable (prior to increment/decrement).
Note that if the result of increment/decrement is not used, there is no difference in which form to use:
let counter = 0;
counter++;
++counter;
alert( counter ); // 2, the lines above did the same
What is the result of these two expressions?
null + 1 = ?
undefined + 1 = ?
null + 1 = 0
null becomes 0 after the numeric conversion
undefined + 1 = NaN
undefined becomes NaN after the numeric conversion