Advanced Javascript Flashcards
What does ‘use strict’ mean?
It turns on a stricter debugging mode.
How do you enable strict mode?
Include:
“use strict”;
at the top of your file or a function. This is ignored by older browsers as it’s just a seemingly random string.
What does strict mode do?
- It doesn’t allow you to use variables you haven’t declared.
- It prevents you from using keywords reserved for future versions of JavaScript.
- It doesn’t allow you to delete variables and functions.
- It doesn’t allow you to delete arguments to functions.
- It makes eval slightly safer by preventing variables from leaking out.
- It prevents the default this value from being assigned to the window object, and instead leaves it undefined.
Does JavaScript pass variables by value or reference?
The primitive types (boolean, number and string) are passed by value. Objects are passed by reference.
What is pass by value?
Passing by value passes a copy of the primitive into the function.
What is pass by reference?
Passing by reference means that it passes a pointer to the original object. This means that we can manipulate props on the object inside the function and it will be changed everywhere.
Note, you can point the reference at a new object in the function, which does not overwrite the old object, just redefines what the variable inside the function is pointing at.
What are the different types in JavaScript?
Primitives: Boolean Number String Null - typeof(null) = "object" Undefined
Object
What is the difference between a dynamically and statically typed language?
In dynamically typed languages (like JavaScript) the types of the variables are determined at runtime and are inferred from the value they currently hold.
What’s the difference between undefined and null?
The JavaScript engine uses undefined to flag variable values that haven’t been set and object properties that are unknown.
Null is set by programmers to show a non-value.
The subtle difference here is that programmers set Null and the engine sets Undefined.
null == undefined is true
What is the difference between == and ===?
=== checks type equality in addition to value
== does type coercion which can give you unintended equalities
What is NaN
It’s for bogus computations
typeOf(NaN) === "number" NaN isn't equal to anything, including itself (NaN === NaN) === false isNaN(NaN) === true isNaN("A") === true (ugh)
so to check if something is NaN... var a = NaN; (a !== a) === true
Because NaN is the only thing not equal to itself.
What are the different scopes in JavaScript?
Global scope (available everywhere) window.foo
Function, or local scope (only available in the function) function moo() { var foo; }
Block scope exists with let and const in ES6 (only in the block)
if (true) {
let foo;
}
What is hoisting?
Hoisting is the automatic hoisting of variable and function declarations to the top of its enclosing scope.
What is the scope chain?
When variable is referenced and it’s not inside the current function scope, it will continually look up through containing scopes to find a definition.
These are lexically defined, so the the function must be contained within the other function or must be passed in.
For instance, this will return variable not defined:
function foo() { console.log(myvar); }
function goo() { var myvar = 1; foo(); }
goo();
What is an IIFE and why might you use it?
Immediately Invoked Function Expression
(function() {
var myCode;
})();
Wraps a section of code in a function to create a scope so variables don’t end up in global scope. We immediately invoke the function, so it will run just as usual but not pollute the rest of our runtime.