JS Logical Questions
JS Logical Questions
Non-mutating methods return a new array or value without modifying the original
array.
The splice() method adds or removes elements in place and returns the removed
items.
The push() method adds one or more elements to the end of the array.
The pop() method removes and returns the last element of the array.
The shift() method removes and returns the first element.
The unshift() method adds one or more elements to the beginning of the array.
The sort() method sorts the elements of the array in place.
The reverse() method reverses the array in place.
The fill() method replaces elements in a specified range with a static value.
The copyWithin() method copies part of the array to another location within the
same array.
● ✅ Delete Properties: Allowed (you can delete keys from the object).
✅ Object.freeze()
● ✅ Reassignment: Allowed (you can reassign the variable, but not mutate
the object itself).
✅ Object.seal()
● ✅ Reassignment: Allowed (you can reassign the variable).
● ✅ Modify Properties: Allowed (you can update existing property values).
OR Operator
In JavaScript, the “OR” operator evaluates the values from left to right and returns the
first truthy value. If none of the values are truthy, the “OR” operator will return the last
operand.
let x = 'Hello' || false; // x is equal to 'Hello' (first truthy value)
let z = false || undefined || 0; // since all are false, z is equal to 0 (the last value)
AND Operator
In JavaScript, the “AND” operator evaluates the values from left to right and returns
the first falsy value. If all the operands are true, the “AND” operator will return the last
operand.
let x = 'Hello' && false; // x is equal to 'false' (first falsy value)
let z = true && 'Hello' && 10; // since all are truthy, z is equal to 10 (the last value)
🔹 Function
● 🎯 Purpose: Performs logic, calculations, or handles data.
● 🔁 Return Value: Can return any data type like number, string, object, etc.
🔹 Component (React)
● 🎯 Purpose: Renders UI and manages state in React.
● 🔁 Return Value: Returns JSX (React elements).
Why?
● The main thread doesn’t wait for await to finish inside the loop.
Type in JS :
In JavaScript, type generally refers to the type of value a variable holds. JavaScript is
a dynamically typed language, meaning variables are not bound to a specific type,
and the type can change during runtime.
Primitive Types
Primitive values are stored in a call stack or a fixed memory location. When you
attempt to "change" a primitive value:
● The original value remains intact in memory.
● A new value is computed and stored in a different memory location.
Example-
let a = 10;
a = 20;
1. Initial Assignment: When you assign let a = 10;, the value 10 is stored in
memory, and the variable a is a reference (or pointer) to that memory location.
2. Reassignment: When you later do a = 20;, JavaScript doesn't overwrite the
original value (10). Instead:
○ A new memory location is allocated for the value 20.
○ The variable a is updated to reference the new memory location.
○ The original value 10 remains in memory until it is garbage collected (if
no other variable references it).
Types are:
It is a unique and immutable value, often used as a key for object properties to avoid
name collisions.
Example :
These are mutable and can hold collections of data or more complex entities:
In JavaScript, the this keyword refers to the context in which a function is executed
— it represents the object that is currently calling the function.