Rithm (Intro to JS) Flashcards

1
Q

What is the difference between JavaScript and ECMAScript?

A

ECMAScript is a standard. JavaScript is an implementation of that standard. The standard dictates certain features and sets of functionality, but there can be different implementations that follow the standard. There are many different JavaScript engines that implement the ECMAScript standard and are competing for dominance; the most popular right now is Google’s V8 engine.

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

Who is the creator of JavaScript?

A

Brendan Eich, who was working at Netscape at the time and now works at Mozilla.

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

When was JavaScript created?

A

May 1995

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

How long did it take to create JavaScript?

A

10 days

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

What was JavaScript called before it was named “JavaScript”?

A

Mocha, a name chosen by Marc Andreessen, founder of Netscape. In September of 1995 the name was changed to LiveScript, then in December of the same year, upon receiving a trademark license from Sun, the name JavaScript was adopted.

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

What are the primitive data types?

A

String, Number, Boolean, undefined, null

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

What is the difference between null and undefined?

A

https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript

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

What does typeof null return?

A

“object”

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

How do you convert a string into a number? Is there a shorthand method to do this?

A

using Number.parseInt() or Number.parseFloat()

parseInt("2"); // 2
parseFloat("2"); // 2
parseInt("3.14"); // 3
parseFloat("3.14"); // 3.14
parseInt("2.3alkweflakwe"); // 2
parseFloat("2.3alkweflakwe"); // 2.3
parseInt("w2.3alkweflakwe"); // NaN (not a number)
parseFloat("w2.3alkweflakwe"); // NaN (not a number)

Using the Number() function or the unary operator (+).

+“2”; // => 2
+“3.14”; // => 3.14
+“2.3alkweflakwe”; // => NaN
+“w2.3alkweflakwe”; // => NaN

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

What does 5 == "5" return? Why?

A

Returns true; the string gets coerced into a number and thus 5 === 5 => true

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

What does "true" == true return? Why?

A

the boolean true gets coerced to a number (1), and then “true” is compared to 1, which returns false.

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

What are the SIX falsey values?

A
  1. 0
  2. ””
  3. null
  4. undefined
  5. false
  6. NaN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happened when you use the Array.prototype.delete() method?

A

delete() does not function the same way shift()/pop() does; delete() will delete the value at the index passed into the method and replace the value with ‘undefined’.

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

What arguments does the Array.prototype.splice() method accept?

A

splice() accepts at least two arguments:

1) the starting index
2) the number of elements to be removed/replaced
3) (optional) an unlimited amount of args to be added into the array

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

What is the syntax for a do .. while loop? What is the difference between that and a regular while loop?

A
let i = 0;
do {
    console.log(i);
    i++;
} while (i < 5);

The code inside a do .. while loop is guaranteed to run at least once.

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

What is the difference between Object bracket notation and Object dot notation?

A

Use the bracket notation when you need to evaluate some expression or pass in a variable to get the name of the key, but when you know the name of the key and it is not a variable or expression, always use the dot notation.

17
Q

How would you determine if a key/property exists in an object?

A

Use a if … in statement.

const obj = {
    favoriteFood: "pizza",
    favoriteColor: "black"
}
if ("favoriteFood" in obj) {
   console.log("property exists!);
} else {
   console.log("property doesn't exist");
}
18
Q

What is scope?

A

MDN defines scope as “The context in which values and expressions are ‘visible,’ or can be referenced”. In JavaScript (before ES2015, which is where we will start), there are only 2 kinds of scope: global scope and function scope.

The important takeaways here are

all variables that are defined outside of functions (and inside of functions without the var keyword) are declared in the global scope, and

all variables defined inside of functions can only be accessed by those functions (and any inner functions).

19
Q

What is an Immediately Invoked Function Expression (IIFE)?

A

As the name implies, an IIFE is a function that immediately gets invoked or called after it is written. To do this, the function is wrapped in parentheses () and then invoked with another set of parentheses.

(function() {
    let str = "Hello, World!";
    return str;
})();

The result of an IIFE can be saved to a variable:

let string = (function() {
    let str = "Hello, World!";
    return str;
})();

console.log(string);

20
Q

What is a common use case for an IIFE?

A

1) Returning an object:

var personObject = (function() {
    return {
        name: "Tim",
        age: 32,
        occupation: "developer",
        hobbies: "sailing"
    };
})();

personObject.name; // returns “Tim”
personObject.age; // returns 32
personObject.occupation; // returns “developer”
personObject.hobbies; // returns “sailing”

2) keeping variables out of the global scope:

var personObject = (function invokeRightAway(){
    var person = "Elie";
    return {
        getName: function(){
            return person;
        },
        setName: function(newName){
            person = newName;
        }
    };
})();

personObject.getName(); // “Elie”
personObject.setName(“Mary”); //
personObject.getName(); // “Mary”
person; // ReferenceError: person is not defined

In this case, we can still access and change the person variable via the personObject.getName and personObject.setName methods. However, if we try to access person directly from outside of the scope of invokeRightAway, we get a reference error.

What is pretty interesting here is that even though invokeRightAway finished running, we were still able to access variables from that function inside of the getName and setName methods. How is that possible? Well, through the use of something called closure.

21
Q

What is “hoisting”?

A

Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it’s declared. This behavior is called “hoisting”, as it appears that the variable declaration is moved to the top of the function or global code.

Example:

instructor // this will NOT throw an error!
var instructor = "Elie";

What’s happening under the hood is:

var instructor;
instructor; // this will NOT throw an error!
instructor = “Elie”;

22
Q

What is the difference in hoisting in function declarations vs function expressions?

A

Function declarations are hoisted, while function expressions are not hoisted.

Function declarations are fully defined before the code is run. Function expressions act differently. Since a function expression assigns an anonymous function to a variable, hoisting applies to that variable name as well.

Function declaration example:

sayHi(“Matt”); // “Hello Matt”

function sayHi(name){
    return "Hello " + name;
}

Function expression example:

sayHi(“Matt”); // Throws an error!

var sayHi = function(name){
    return "Hello " + name;
}

There is an error because we are trying to invoke a function called sayHi but at that point in the code sayHi is not equal to a function. In fact, sayHi exists in memory but it is undefined. The above code is equivalent to the following:

var sayHi;
sayHi(“Matt”); // Throws an error because undefined is not a function (we are using the type of undefined and trying to invoke it - that’s why we get a TypeError)

sayHi = function(name){
return “Hello “ + name;
}