More JavaScript Concepts Module#26 Flashcards
What are the 3 kinds of scope?
Global, Block, Function
What is closure
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.
What happens if a variable is used without being declared first while in non-strict mode?
The variable gets attached to the global scope which can be a gnarly source of bugs.
Describe “hoisting” in JavaScript.
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.
Which mode should we default to when writing our JS?
Strict mode
Why are immediately invoked function expressions useful?
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 */)( ).
When you see this error: TypeError: console.log(…) is not a function what’s “usually” the problem?
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 would you convert another primitive value to a string?
By invoking the toString( ) method on the value that you are targeting.
There are several other ways to convert boolean values to strings. Consider these real quick»»»»»
var truthNess = true var falseNess = false
String(truthNess) //”true”
truthNess.toString( ) //”true”
String(falseNess) //”false”
falseNess.toString( ) //”false”
Casting from Date to String
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 do you convert a String into a number?
By calling the Number( ) global function. See below.
Number(“1”) //1
Number(“0”) //0
Cast a boolean into a number. Think binary.
Number(true) //1
Number(false) //0
Cast a date into a number primitive
Number(null) //0
Number(undefined) //NaN
Number(NaN) //NaN
ANY value can be converted into a Boolean
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
What is the value of this while in strict mode?
Outside any object, this in strict mode is always undefined. Remember functions are objects too.