Declarations Flashcards
Syntax to declare variable with block scope? (2)
Let - declares a block scope local variable, optionally initializing it to a value. *Let cannot declare a variable at global scope.
Const - declares read-only variable with block scope
Limitations to variables declared with ‘Let’
- not hoisted; temporal availability when block is executed
- cannot create variable on global object
ex: Cannot check properties outside of block; cannot access within block statements like Switch{ } - backwards compatibility issues (ES6+)
When are double quotes required in JavaScript?
JSON requires double quotes for strings
What is wrong with this syntax?
var a=b=c=10 ;
b and c are global variables. oops
- Do not chain var declaration AND assignments
var a, b, c; //ok
What is the scope of a parameter passed to a function?
Parameters are local variables (no need to re-assign to var within the function body).
- A parameter passed to a parent function will be in closure scope (is known to IIFE it returns)
Are identifiers case-sensitive?
Yes
“typeof” operator applies to?
Primitives
“instanceof” operator applies to?
Objects
What is a statement, and how can it be identified?
Statements “do things”. A sequence of statements is a program.
To prevent ambiguity, terminate statements with semicolon ;
What is the diff between a statement and an expression? Are they interchangeable?
Expressions produce values
- Expressions can be used where a statement is expected (“expression statement” ends with a ‘;’)
How does JS differentiate a block statement from an expression?
- Expressions cannot start with curly brace { }
- Expressions cannot start with function keyword
- Block statements do NOT end with semi-colon
The following are block statements:
{
foo: bar(3, 5)
}
function foo() { }
How do you turn a block statement into an expression? Examples?
( ) can transform blocks into expressions
ex.1: Object literal ({dog:Floyd})
Create function expression ending with semicolon var f = function( ){ };
Create IIFE with parentheses and semicolon
(function( ) { …})( );
What is ‘lexical scoping’ ?
The word “lexical” refers to the fact that lexical scopinguses thelocation where a variable is declared within the source code to determine where that variable is available.Nested functions have access to variables declared in their outer scope.