Vanilla JavaScript Flashcards
Can you use the ternary operator with an if else structure?
Yes you can
let test = false ? console.log("flag 1") : true ? console.log("flag 2") : null;
In this example test will contain the return value of undefined from console.log( ).
What is a JavaScript expression? Give some examples.
A code snippet that evaluates to a value.
0 // 0
1 + 1 // 2
‘Hello’ + ‘ ‘ + ‘World’ // ‘Hello World’
{ answer: 42 } // { answer: 42 }
Object.assign({}, { answer: 42 }) // { answer: 42 }
answer !== 42 ? 42 : answer // 42
answer = 42 // 42
What is a JavaScript statement? Give some examples.
A code snippet that performs an action, it does not evaluate to a value.
// `if` statement if (answer !== 42) { answer = 42 } // `for` is a statement for (;;) { console.log('Hello, World'); } // Declaring a variable is a statement let answer = 42
What must a JavaScript functions arguments be and not be? Give examples of both.
Function arguments must be expressions they cannot be statements.
A function argument can not be an if statement, for loop
or variable declaration.
A function argument could be an object, a ternary expression, a string, a variable assignment, an array.
What is a IIFE and how do you do create one?
Immediately Invoked Function Expression
You wrap the function in parenthesis and add a set of () parenthesis after this.
( function getAnswer( ) { return 42 } )( );
This function will be invoked immediately.
What are parenthesis ( ) also know as in JavaScript? What is their purpose?
The grouping operator ( ) which controls the precedence of evaluation in expressions.
What does a closure allow a function to have?
private variables
How do you create a function with private variables?
First create an initial function that declares some variables in its body. This initial function will return another function that accesses the variables.
Call the initial function assigning the returned function to a variable.
Then call the returned function, this has access to the variables in the initial function that created it. This is called a closure.
Only the returned function can access the variables inside the initial function.