More JavaScript Concepts Module#26 Flashcards

1
Q

What are the 3 kinds of scope?

A

Global, Block, Function

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

What is closure

A

Source: MDN
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

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

What happens if a variable is used without being declared first while in non-strict mode?

A

The variable gets attached to the global scope which can be a gnarly source of bugs.

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

Describe “hoisting” in JavaScript.

A

Before executing the code a person writes, the JavaScript engine reorders it according to some rules. For example, functions are moved to the top of their scope.

This is why this is legal to write:
dosomething()
function dosomething( ){
console.log("did something") }

The function is “hoisted” above the call before execution.

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

Which mode should we default to when writing our JS?

A

Strict mode

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

Why are immediately invoked function expressions useful?

A

Because they don’t pollute the global object.

This is how you should be writing an IIFE:
( function( ) {
//do something now
} )( )

Arrow function format:
( ( ) => { // do something now } ) ( )

We basically have a function defined inside parentheses, and then we append () to execute that function: (/* function */)( ).

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

When you see this error: TypeError: console.log(…) is not a function what’s “usually” the problem?

A

JavaScript sees a second opening parenteses, and thinks you’re trying to invoke console.log(‘test’), doing console.log(‘test’)().

Solution is to add a semi ; to the beginning of the second line of the code. Example:

console.log('test')
;(function( ) {
 /* */
})( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you convert another primitive value to a string?

A

By invoking the toString( ) method on the value that you are targeting.

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

There are several other ways to convert boolean values to strings. Consider these real quick»»»»»

A
var truthNess = true
var falseNess = false

String(truthNess) //”true”
truthNess.toString( ) //”true”
String(falseNess) //”false”
falseNess.toString( ) //”false”

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

Casting from Date to String

A
String(new Date('2019-01-22'))
//"Tue Jan 22 2019 01:00:00 GMT+0100 (Central European Standard Time)"
(new Date('2019-01-22')).toString()
//"Tue Jan 22 2019 01:00:00 GMT+0100 (Central European Standard Time)"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you convert a String into a number?

A

By calling the Number( ) global function. See below.

Number(“1”) //1
Number(“0”) //0

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

Cast a boolean into a number. Think binary.

A

Number(true) //1

Number(false) //0

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

Cast a date into a number primitive

A

Number(null) //0
Number(undefined) //NaN
Number(NaN) //NaN

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

ANY value can be converted into a Boolean

A
Here we go using the Boolean( ) global function:
Boolean(false) //false
Boolean(0) //false
Boolean(NaN) //false
Boolean("") //false
Boolean(null) //false
Boolean(undefined) //false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the value of this while in strict mode?

A

Outside any object, this in strict mode is always undefined. Remember functions are objects too.

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

How do you enable strict mode in your JS file?

A

To invoke strict mode for an entire script, put the exact statement “use strict”; (or ‘use strict’;) before any other statements.

// Whole-script strict mode syntax

'use strict';
var v = "Hi! I'm a strict mode script!";

Strict mode for modules:

function strict() {
    // because this is a module, I'm strict by default
}
export default strict;
17
Q

Refresher! What is a callback?

A

A callback is a function passed as an argument to another function. This technique allows a function to call another function.A callback function can run after another function has finished.

Consider the below:

function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}
function myCalculator(num1, num2, myCallback) {
  let sum = num1 + num2;
  myCallback(sum);
}

myCalculator(5, 5, myDisplayer);

The function is being is executing a callback which has been passed as a parameter and then the callback is passing the output of the function as a parameter.

18
Q

What are the rules for automatic semi-colon insertion? This is !important

A
  1. When the next line starts with code that breaks the current one (code can spawn on multiple lines)
  2. When the next line starts with a }, closing the current block
  3. When the end of the source code file is reached
  4. When there is a return statement on its own line
  5. When there is a break statement on its own line
  6. When there is a throw statement on its own line
  7. When there is a continue statement on its own line
19
Q

Under what circumstances is a switch statement appropriate?

A

Something that would require numerous if/else chains. The syntax is below for review. This is exactly the way it functions in PHP.

const a = 2
switch(a) {
  case 1:
    //handle case a is 1
    break
  case 2:
    //handle case a is 2
    break
  case 3:
    //handle case a is 3
    break
  default:
    //handle all other cases
    break
}