Important Javascript facts Flashcards
Explain Javascript primitives
Primitives are immutable, that is, they can only be changed by adding additional expressions for example:
var someValue=1;
someValue=2;
someValue will now be 2.
These are string, number, boolean, null and undefined. In essence, it excludes functions, objects and arrays.
Explain Javascript non-primitives
Objects , functions and arrays are non-primitives. Non-primitive values are mutable, that is, it can be modified after it is created without adding additional expressions.
For example:
Arrays can have keys pushed or popped. Indexes can have their values overwritten.
Object keys can be added and changed using dot or bracket notation.
This allows for an issue to occur, where if one reference to one of these heap items are updated, then the values are updated on all references to it.
What is an expression?
A line of code in which a value is assigned to a variable. These values can also be functions, and are then called function expressions.
Explain function expression vs function declaration
Declaration:function myFunction(){};
Expression: var myFunction=function(){}
Describe the problem with mutabality.
Non-primitives are stored in the heap when they are created. If this item is mutated in the heap, then any variable referencing this item in the heap will also be updated. Objects, functions and arrays can be mutated via dot notation, bracket notation (objects and functions) or pushing, popping, overwriting index values (arrays).
We make shallow/deep copies/clones to prevent the problem. Deep copies would be best, but maybe less performant when they are created.
`
Example of issue
const person = {
name:’Albert’,
attributes:{
hairColor:null,
age:36,
}
}
const person2 = person;
/*
Now we update the heap
*/
person.name=’Lucas’;
person.attributes.age=20;
/*
person and person2 point to the same object in the heap. When person one is mutated, then person2 is also mutated, by accident
*/
console.log(person2.name) // will also be Lucas
console.log(person2.attributes.age) // will also be 20
`
When is a variable undefined?
Variables that are declared but have not been assigned a value (var myCar) has a value of undefined. That is, not initialized even though it’s been declared - hoisting
When a property that is accessed does not exist on the object
When a function returns nothing, then a variable assigned to its invocation will be undefined
Explain Javascript hoisting and which variable types are hoisted.
Any variable declarations (when variables are created, also function expressions) are hoisted to the top of the function or script. Not const or let, just var.
This means that such a variable can be used before it is declared.
If used before declared, the value will be undefined because only declarations are hoisted. Value assignments/initializations are not.
Function declarations are hoisted as well
Which variable types should be copied and what is the difference between a shallow copy and a deep copy.
Arrays and objects only because it is tightly coupled with mutability and the way this data is updated in the heap and referenced by other variables.
With both shallow and deep copies other variables that reference the same item in the heap will no longer be changed when the cloned object/array is changed.
Shallow copies:
Only the case for top level items. Nested objects/arrays are still problematic.
Deep copies:
The case for all items, no matter how deeply the objects/arrays are nested.
How to make a shallow copy of an object.
Using the rest operator:
var {…duplicate}=oldObject;
Using the spread operator:
var newObject={…oldObject};
Using Object.assign:
var newObject=Object.assign({},oldObject);
How to make a shallow copy of an array
Using the rest operator:
var […duplicate]=oldArray;
Using the spread operator:
var newArray=[…oldArray];
Using .slice:
var newArray=oldArray.slice(0);
How to make a deep copy of objects and arrays
The stringify and parse approach won’t copy methods, which in most cases is a non-issue
var newObject=JSON.parse(JSON.stringify(oldObject));
var newArray=JSON.parse(JSON.stringify(oldArray));
OR
Lodash or similar
Example object literal
var objectLiteral={
key:”value”,
key2:”value”,
doSomething:function(){
}
}
null vs undefined vs not defined
If you as a developer want a value to be non-existent then use null.
If a variable is assigned a value of nothing, then Javascript itself will give it a value of undefined, for example if it declared but not assigned a value. You as a developer should not manually set it as undefined.
not defined is when a variable was not declared at all. If referencing this in your code then it will give an error.
What is a function/method signature
The arguments it expects, along with it’s types. May also include the return value and it’s type
What is HOF (Higher Order Function)
A function or method that takes a callback function as an argument or returns a function or both.
What are falsy and truthy values
Falsy:
null
undefined
false
0
NaN
empty string: “”, ‘’ or ``
Anything else is truthy
How to negate a value
Use an exclamation in front of the variable for example !someVariable
What is the purpose of double negation
Turns truthy values to boolean true and falsy values to boolean false.
Explain nullish coalescence
const someVar = null;
const newVar = someVar??’other-value’
If somevar is null OR undefined, use ‘other-value’ instead
Explain super keyword
This is used in a child class’s constructor, to send data up to it’s parent class’s constructor.
If a child class is instantiated, then in it’s constructor super()
must be invoked as the first line. If no data is meant to be sent to the parent, just leave it empty. If you want to send data up, add it between the paranthesis.
Explain optional chaining
If the value, on which properties or methods are being accessed, is not an object but is null or undefined, then short-circuit the expression to have a value of undefined.
example
const person =null;
const thisValue = person?.role?.name;
thisValue will be undefined if role is null/undefined or person is null/undefined
Also functions if the function is nullish
myFunction?.() is the same as
If(myFunction) myFunction()
Explain ES6
The term ES6 refers to the newer Javascript versions. It started in 2015 and every year it is extended. Referring to ES2015 refers to the first new version, and ES2020 refers to the version released in 2020. Babel can transpile ES6 to ES5 (old Js) so that browsers can understand it. Some features are now natively supported without transpilation. Gulp, web pack, Parcel etc all use Babel for transpilation. babelrc file is required, which lists all the presets (grouped plugins), plugins and custom configuration. The output will be a single Javascript file that can be included in the HTML.