JavaScript Flashcards
What are the DATA TYPES of JavaScript? (there are 8)
PRIMITIVE: string, number, boolean, BigInt, symbol, undefined, null,
NON-PRIMITIVE: object
What is HOISTING
Hoisting is a default behavior of JavaScript where all the variable and function declarations are moved on top. Declarations can be hoisted in the global or local scope. Only declarations are hoisted, not initializations. Also, function expressions (functions assigned to a variable, including arrow functions) are not hoisted.
What is the difference between == and === ?
Both are comparison operators. The difference between both the operators is that,“==” is used to compare values whereas, “ === “ is used to compare both value and types.
What is IMPLICIT TYPE COERCION?
The automatic conversion of value from one data type to another. Takes place when the operands of an expression are different data types.
What values are FALSY in JS? (there are 8)
false, 0, 0n, -0, “”, null, undefined and NaN
What TYPE of language is JS
JS is a dynamically (loosely) typed language. The type of variable is checked on RUN-TIME vs COMPILE-TIME with statically typed programs. Also, variables can change their data type if reassigned.
What is NaN?
Means “not-a-number”. Can be used to check if a value is not a legal number by using isNaN( ) and passing the value as an argument.
What is passed by VALUE vs. passed by REFERENCE?
Primitive data types are passed by value and non-primitive are passed by reference. This means that when a primitive variable is assigned to another primitive variable, it will create a copy of that value: var x = 23; var y = x; var x = 24; // var y is still equal to 23 because it is made as a separate value.
With non-primitive data-types (objects), the assignment operator is creating a new variable name for the object, where the new variable name points to the same object it was assigned to, and not a copy:
var obj1 = { a: 1, b: 2 }; var obj2 = obj1; obj1.c = 3 // obj1 also has the c: 3 property because it's referring to the same object.
What is an IMMEDIATELY INVOKED FUNCTION in JS?
Known also as IFFE/IFFY (“iffy”). A common JavaScript pattern that executes a function instantly after it’s defined. Developers primarily use this pattern to ensure variables are only accessible within the scope of the defined function. Created by enclosing the function with parentheses and adding parenthesis after the closed parenthesis of the function.
What are HIGHER ORDER FUNCTIONS?
Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.
Explain the THIS keyword
The “this” keyword refers to the object that the function is a property of. If the function is not part of an object, then ‘this’ refers to the global object, or Window object.
Explain .call( );
The .call( ) method is actually what is being used when you invoke a function. So, when you add the parenthesis after a function to call it, this is actually the method that is used behind the scenes.
.call( ) can also be used if you want to use a property from another object on some other object that doesn’t have that property:
const player1 = { name: Bob, battery: 70, chargeBattery: ( ) => this.battery = 100 }; const player2 = { name: Sue, battery: 80 }; player1.chargeBattery.call(player2) // This says take the chargeBattery function from player1 and apply it to player2
.call( ) can also take arguments after the object you are calling.
Explain .apply( );
The apply method is similar to the .call( ) method, except that if it takes arguments in addition to the called object, those arguments are passed as an array of values.
Explain .bind( );
The bind() method creates a new function that, when called, has its ‘this’ keyword set to the provided argument (which is an object):
const person1 = { name: 'Sam' } const person2 = { name: 'Saul' } function sayHi( ) { console.log( this.name + ' says hi!') } const samSaysHi = sayHi.bind(person1); samSaysHi( ) // returns 'Sam says hi!' because the function sayHi was bound to the person1 object and initialized as the function samSaysHi. Then, it was called.
What is CURRYING?
Currying takes a function that has more than one parameter and breaks it into many unary functions (or functions that have only one parameter). It then calls each function with the separate parameters one at a time.
function add(a) { return function(b) { return a + b; } } add(3)(4)