Hoisting is a behaviour in JavaScript where variable and function declarations are moved to the top of their respective scopes during the compile phase. This means you can use variables and functions before they are declared in the code. However, only the declarations are hoisted, not the initialisations.
var: The declaration is hoisted, but the variable is initialized to undefined until the line where it is assigned a value.let and const: The declarations are hoisted, but the variables remain uninitialized and are in the Temporal Dead Zone (TDZ) until the code execution reaches their declaration. Accessing them before declaration results in a ReferenceError.var, it will be undefined before the assignment. If using let or const, it will be in the TDZ.console.log(a); // undefined (var is hoisted, but uninitialized)
var a = 10;
console.log(b); // ReferenceError (let is hoisted but in TDZ)
let b = 20;
foo(); // Works fine (function declaration is hoisted)
function foo() {
console.log("Hello");
}
bar(); // TypeError (function expression is hoisted as undefined)
var bar = function() {
console.log("Hi");
};
Both are comparison operators. The difference between both the operators is that “==” ( loose equality ) is used to compare values whereas, “ === “ ( strong equality ) is used to compare both values and types.
Implicit type coercion in JavaScript is the process where JavaScript automatically converts one data type into another when performing operations involving different types. This usually happens when using operators like +, -, ==, etc., on operands of different types.
Using the + Operator:
If one operand is a string and the other is a number, JavaScript converts the number to a string, and the + operator performs concatenation rather than addition.
5 + '3'; // Output: '53' (number is coerced into a string)
Using The + operator:
The + operator only works with numbers. If one of the operands is a string, JavaScript attempts to convert the string to a number. If the string contains a valid number, subtraction occurs; otherwise, the result is NaN.
'5' - 3; // Output: 2 (string '5' is coerced into number 5)
'hello' - 3; // Output: NaN (since 'hello' cannot be converted to a number)
Boolean Coercion (Truthy/Falsy Values):
JavaScript automatically coerces values into booleans when necessary. Values like false, 0, 0, 0n, "", null, undefined, and NaN are considered falsy. All other values are truthy.
if ('') { console.log('This won’t run'); } // Falsy
if ('hello') { console.log('This will run'); } // Truthy
Logical Operators (|| and &&):
In JavaScript, the logical operators || and && return the actual values of operands, not just true or false.
|| (OR) returns the first truthy value or the last value if none are truthy.console.log(0 || 'hello'); // Output: 'hello' (since 0 is falsy)
console.log('world' || 'hello'); // Output: 'world' (since 'world' is truthy)
&& (AND) returns the first falsy value or the last value if both are truthy.console.log(0 && 'hello'); // Output: 0 (since 0 is falsy)
console.log('world' && 'hello'); // Output: 'hello' (since both are truthy)