JavaScript Flashcards
What is JavaScript?
JavaScript is a single-threaded, multi-paradigmed (OOP and FP), interpreted, programming language
What is the difference between let
, const
, and var
?
let
and const
are blocked scoped. var
is function scoped no matter how deeply it may be nested inside that function.
What is the difference between a first-class and higher-order function?
A first-class function is a function that can be used just like any other variable. For example, a first-class function can be assigned to a variable, passed as an argument to another function, and even even be returned by a function. A higher-order function is a subset of a first-class function; it can only be passed to and returned by other functions
What is JS’s most popular execution environment?
The browser
What are the 7 primitive types and what are their associated bits?
- number (64 bit double)
- bigInt (arbitrary bit integer)
- boolean
- string (16 bit unicode)
- null
- undefined
What is the result of the following code:
NaN === NaN
The result is false. This is because NaN
is not equal to any other value, including itself. Use Number.isNaN(x)
instead
What is the behavior of ===
for primitives and reference types?
Primitives:
- Test to see if 2 values are the same
Reference Types:
- Test to see if 2 reference variables point to the same object in the heap
Are strings iterable?
Yes
What are the 2 ways to access a character of a string at a given index?
1. str.charAt(index); 2. str[index];
Code an example of a template literal
let name = "Gianmarco"; Console.log(`Hello, ${name}!`)
Which 5 values evaluate to false and which values evaluate to true?
Evaulate to false:
* null
* undefined
* NaN
* 0
* “”
Evaluates to true:
* Everything else
What is the global object for all JS code inside a browser?
Window
What is the default value for uninitialized variables?
undefined
Code an example of a destructuring assignment
let [x, y] = [1, 2]; // variable names do not have to match console.log(x, y); // 1 2 let {x, y} = {x: 0, y:0} // variable names must match, else undefined console.log(x, y) // 0 0
Code an example of the 2 different ways to access an object’s property
let obj = {x:0, y:0}; console.log(obj["x"]); // 0 console.log(obj.x); // 0
What is the behavior of optional chaining?
a?.b;
If a
is not null
or undefined
, access b
. Else, return undefined
All objects are instances of which class?
Object
What do the following expressions return?
1 && 2; 0 && 1; 1 && 0;
1 && 1
returns 2
. This is because if both values are true, the right operand is returned
0 && 1
returns 0
. This is because if the first value is false, the expressions short circuits and returns it
1 && 0
returns 0
. This is because 1
is true so the operator returns the right operand
Code a function using a default parameter value
function f(x = 0) {...}
What is the behavior of the nullish coalescing operator?
a ?? b;
If a
is null
or undefined
, return b
. Else, return a
What does the following code return?
let result = "x" in {x: 0}; console.log(result);
function
What is the behavior of the following code?
let obj = {x:0, y:0}; delete obj.x; console.log(obj);
Deletes the x
property of obj
{y:0}
What is the behavior of the following code?
let arr = [1,2,3]; delete arr[0]; console.log(arr.length); console.log(typeof arr[0]);
Deletes the first element of arr
causing it to become a sparsed array. It’s length will remain at 3 but its first element will be undefined
Code 2 examples using the in
operator. 1 for an array, and the other for an object
let arr = ["a","b"]; console.log(0 in arr); // true let obj = {x:0, y:0}; console.log("x" in obj); //true
What is the difference between the for/of and for/in loop?
The for/of loop iterates over the elements of an iterable and therefore, cannot be used on non-iterable objects. The for/in loop iterates over the properties of both iterables and non-iterable objects