JavaScript Fundamentals Flashcards

1
Q

Can you overwrite variable declarations without an error using var?

A
Yes;
var camper = 'James';
var camper = 'David';
console.log(camper);
// logs 'David'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the six falsy values?

A

undefined, null, NaN, 0, “” (empty string), and false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Will these return true or false?

1) false == “”
2) NaN == undefined
3) 0 == null
4) null == undefined
5) NaN == NaN
6) 0 == “”

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 7 primitive data types of JavaScript?

A
undefined
Boolean
Number
String
BigInt
Symbol
Null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is Math.max() smaller than Math.min()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the differences between var, let, and const?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are things not allowed in ‘use strict’

A

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):
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if you assign a variable without a keyword?

A

If not using strict mode, it will be assigned to the window. So x = 1 will be window.x = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a ReferenceError and name common examples.

A

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 ==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a SyntaxError and name some examples.

A
  • 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 ,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a type error and name some examples.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does this return?

alert(“1” + 2 + 2);

A

“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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does this return?

alert(2 + 2+ “1”);

A

“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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
let counter = 1
let a = ++counter;
****************
let counter = 1
let a = counter++;

What is the value of a in each situation and why?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the result of these two expressions?
null + 1 = ?
undefined + 1 = ?

A

null + 1 = 0
null becomes 0 after the numeric conversion

undefined + 1 = NaN
undefined becomes NaN after the numeric conversion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the result of the following expressions?

console. log(“2” > 1);
console. log(“01” == 1);

A

Both are true because when comparing values of different types, JavaScript converts the values to numbers

17
Q

What does this do?

console.log(!!”non-empty string”)

A

Returns true. A double NOT !! is sometimes used for converting a value to a boolean type
Same as:
console.log(Boolean(“non-empty string”)); //true

18
Q

What will this result be?

console.log(null || 2 && 3 || 4)

A

The precedence of AND && is higher than ||, so it executes first.

&& returns the first falsy value or the last value if no falsy is found.

The result of 2 && 3 = 3, so the expression becomes:
null || 3 || 4
Now the result is the first truthy value: 3

19
Q

When would you want to use “do…while” over “while”?

A

This form of syntax should only be used when you want the body of the loop to execute at least once regardless of the condition being truthy. Usually, the “while” is preferred

20
Q

What is the nullish coalescing operator ?? and how is it differ from the OR || operator?

A

|| returns the first truthy value.
?? returns the first defined value.

We use ?? when we want to use default value only when the variable is null/undefined (instead of false, 0, “”). That is, when the value is really unknown/not set.

21
Q

What are rest and spread parameters?

A
  • When … is at the end of function parameters, it’s “rest parameters” and gathers the rest of the list of arguments into an array.
  • When … occurs in a function call or alike, it’s called a “spread syntax” and expands an array into a list
22
Q

What is optional chaining?

A

The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn’t exist.

The ?. checks the left part for null/undefined and allows the evaluation to proceed if it’s not so. If not it returns undefined. This allows to safely access nested properties.

The syntax has 3 forms:
obj?.prop – returns obj.prop if obj exists, otherwise undefined.
obj?.[prop] – returns obj[prop] if obj exists, otherwise undefined.
obj.method?.() – calls obj.method() if obj.method exists, otherwise returns undefined.

23
Q

What is a programming paradigm?

A

A strategy for designing and implementing your code. A philosophy, style, or general approach to writing code.

24
Q

What type of language is JavaScript?

A

JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. JavaScript is a prototype-based, multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.

As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative programming styles. It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM).

25
Q

What does this return?

console. log( ‘01’ == 1 );
console. log( true == 1 )

A

True. When comparing values of different types, JavaScript converts the values to numbers.
True. For boolean values, true becomes 1.

26
Q

What is the difference between == and ===?

A

Operands of different types are converted to numbers by the equality operator ==.

A strict equality operator === checks the equality without type conversion.

27
Q

a()

function a() {
console.log('hi')
}
function a() {
console.log('bye')
}
A

‘bye’

With variables, they’re partially hoisted so we have undefined to that variable.

However….

during the hoisting phase, we fully hoist function declarations!

28
Q

What logs?

var favoriteFood = “grapes”;

var foodThoughts = function () {
     console.log(favoriteFood);
     var favoriteFood = "sushi"
     console.log(favoriteFood);
}

foodThoughts()

A

undefined
sushi

Hoisting on every execution context. Every time we run a function, a new execution context get created and we have to go through the creation phase and execution phase again.

favoriteFood gets hoisted within the foodThoughts function, so when the first console.log happens, favoriteFood within that function is initialized but undefined.

If we remove the var from the function and simply had favoriteFood = “sushi”, then the first console.log would be grapes.

29
Q

What will be the output of the code below?
var z = 1, y = z = typeof y;
console.log(y);

A

The output would be undefined. associativity of the assignment operator is Right to Left, so typeof y will evaluate first , which is undefined. It will be assigned to z, and then y would be assigned the value of z and then z would be assigned the value 1.