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.
How do you enable strict mode in your JS file?
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;
Refresher! What is a callback?
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.
What are the rules for automatic semi-colon insertion? This is !important
- When the next line starts with code that breaks the current one (code can spawn on multiple lines)
- When the next line starts with a }, closing the current block
- When the end of the source code file is reached
- When there is a return statement on its own line
- When there is a break statement on its own line
- When there is a throw statement on its own line
- When there is a continue statement on its own line
Under what circumstances is a switch statement appropriate?
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 }