fundaments Flashcards
Explain the difference between var, let, and const.
var:
Function-scoped (or globally scoped if not within a function).
Can be re-declared and reassigned.
Hoisted to the top of the function/global scope.
let:
Block-scoped.
Can be reassigned but not re-declared within the same scope.
Not hoisted.
const:
Block-scoped.
Cannot be reassigned or re-declared.
Not hoisted.
What are primitive data types in JavaScript?
Number
String
Boolean
Null
Undefined
Symbol (ES6)
BigInt (ES10)
What is the difference between == and ===
==: Performs type coercion (attempts to convert operands to the same type before comparison).
===: Strict equality comparison.
(Returns true only if both operands are of the same type and have the same value)
Explain how hoisting works in JavaScript.
Hoisting is a JavaScript behavior where variable declarations are moved to the top of their scope (function or global) during the compilation phase.
var declarations are hoisted, but their initial value remains undefined.
Function declarations are also hoisted.
Describe how if, else if, and else statements work
if: Executes a block of code if a specified condition is true.
else if: Executes a block of code if the previous if condition is false and the current else if condition is true.
else: Executes a block of code if none of the preceding if or else if conditions are true.
Explain how for, while, and do…while loops work.
for: Executes a block of code a specified number of times.
while: Executes a block of code as long as a specified condition is true.
do…while: Executes a block of code
once, then repeats the execution as long as a specified condition is true.
What is a ternary operator?
The ternary operator ( ? : ) is a concise way to write a conditional expression.
condition ? expressionIfTrue : expressionIfFalse
Explain how to define a function in JavaScript.
function functionName(parameter1, parameter2) {
// code to be executed
return value;
}
What are arrow functions and how do they differ from regular functions?
Arrow functions provide a more concise syntax for writing functions.
They have a different this binding compared to regular functions.
What is the purpose of the this keyword in JavaScript?
this refers to the object that the function is a property of.
Its value can change depending on how the function is called.
How do you create an array in JavaScript?
let array = [element1, element2, element3];
let array = new Array(size); // Creates an array with the specified size
List some common array methods.
map(): Creates a new array by applying a function to each element of the original array.
filter(): Creates a new array with elements that pass a test provided by a function.
reduce(): Applies a function to an accumulator and each element in the array (from left to right) to reduce it to a single value.
forEach(): Executes a provided function once for each array element.
How do you iterate over an array in JavaScript?
for loop
for…of loop
forEach() method
How do you create an object in JavaScript?
let object = {
property1: value1,
property2: value2
};
How do you access and modify object properties?
Access:
object.property
object[‘property’]
Modify:
object.property = newValue
object[‘property’] = newValue