Vanilla JavaScript Flashcards

1
Q

Can you use the ternary operator with an if else structure?

A

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( ).

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

What is a JavaScript expression? Give some examples.

A

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

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

What is a JavaScript statement? Give some examples.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What must a JavaScript functions arguments be and not be? Give examples of both.

A

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.

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

What is a IIFE and how do you do create one?

A

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.

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

What are parenthesis ( ) also know as in JavaScript? What is their purpose?

A

The grouping operator ( ) which controls the precedence of evaluation in expressions.

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

What does a closure allow a function to have?

A

private variables

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

How do you create a function with private variables?

A

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.

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