Variables Flashcards
What is considered a variable name in JS? what is not?
- Variable names declared by let or var
- constant name declared by const
- Function names
- Function parameters
- Class names
Property names of objects are generally considered identifiers but NOT variable names. The exception to this is property names of the global object.
What is the return value of an assignment?
the right hand side of the ‘=’.
But be careful, declarations, even with assignment, are statements and don’t have return values.
How are primitive variables and objects/arrays passed in JavaScript?
Primitive variables in JavaScript are passed by value, while arrays and objects are passed by reference.
What does it mean when a variable is hoisted in JavaScript?
All variables in JavaScript declared with var are hoisted, meaning they are virtually moved to the beginning of the scope.
What scope do undeclared variables have?
All undeclared variables in JS (created without the use of let or const) have global scope.
Can you mutate passed objects within a function in JavaScript?
While you can mutate passed objects within a function, reassignment is not a destructive operation. Reassigning a passed object within a function will NOT mutate the passed object. This is because the object within the function is a copy of the pointer to the passed object. If we reassign to this COPY it changes the pointer stored in the copy to point at the new object without changing what the original pointer points at.
What is global scope?
Any function or code block within your program can read and modify variables with global scope.
How is global scope created in JS?
Variables declared in global scope are typically defined outside of any functions or code blocks. Variables declared without the use of let or const are also have global scope.
What is local Scope?
Variables in local scope are typically declared within functions, conditional statements, loops, or other code blocks. They are “local” to that block of code, and they cannot be directly accessed from outside it.
How is local and block scope different?
Local scope is function-level, meaning it encompasses the entire function, while block scope is limited to the specific block where the variable is declared.
What is a block?
A block is a related set of JavaScript statements and expressions between a pair of opening and closing curly braces. These include if statements, loops, switch, etc. function bodies are not technically blocks but we can think of them as such for scoping purposes. Object literals are NOT blocks.
what is variable shadowing?
Variable shadowing occurs when a variable with the same name is declared in an inner scope, such as a function or a block, as a variable in an outer scope. In such cases, the variable in the inner scope shadows or hides the variable in the outer scope.
How should you declare variables in JS?
variables should be declared with let or const