The React Handbook Flashcards
Are variables in JavaScript typed?
No, once you assign a specific literal type to a variable, you can later reassign the variable to house another type without any type errors or issues.
What is the scope?
The scope is the portion of code where a variable is visible.
How is a variable initialized with ‘var’ outside of a function assigned?
It is assigned to the global object and has a global scope and is visible anywhere.
How is a variable initialized inside a function assigned?
It is assigned to that function. It’s local and is visible only inside the function, just as it it were a function parameter.
Does a block (Identified by a pair of curly braces) define a new scope for a variable defined by ‘var’?
Nah, dawg. A new scope is only created when a function is created, because var does not have block scope but function scope.
What is hoisting?
JS actually moves all variable declarations to the top before executing the code. This is why variables defined anywhere in the function are available, even in lines of code above them. To avoid “what the heck is that variable?” types of confusion, however, we should always declare our variables at the beginning of a function.
What is let?
It’s a block-scoped version of var - that is, its scope is limited to teh block, statement or expression where it’s defined, and all the contained inner blocks
What happens if you define let outside of any function?
Unlike var, it does not create a global variable.
What’s unique about a const, once it is initialized?
Its value can’ tbe changed again and it can’t be reassigned to a different value.
What’s the catch about reassigning consts?
We can’t reassign a const, but we can mutate it, if it is an object that provides methods to mutate the object’s contents.
What sort of scope does Const have?
Block scope, same as let.
Why would a developer choose to only use const and let?
Because we should always use the simplest construct available to avoid errors down the road.
Convert the following to an arrow function:
const myFunction = function() { //... }
const myFunction = () => { //... }
When can you omit the curly braces and write everything on a single line, like this: const myFunction = () => doSomething();
Whenever the function body is just a single statement.
When can you omit the parentheses completely? Like this: const myFunction = param => doSomething(param)
When you have one (and only one) param.
What sort of returns are you allowed with arrow functions?
Implicit returns - that is, within an arrow function, values are returned without using the return keyword.
When can you use implicit returns within an arrow function?
When there is a one-line statement for the function body. So,
const myFunction - () => ‘test’
will return ‘test’, even though we don’t use the keyword.