How can you check if a variable x is equal to 10 in JavaScript?
var, let, const
How can you check if a variable x is equal to 10 in JavaScript?
if (x === 10) { /* code */ }
What is the purpose of a for loop in JavaScript?
To repeatedly execute a block of code a specific number of times.
What is the scope of a variable declared with the let keyword?
Block scope
What is the purpose of the else statement in an if-else statement?
It provides an alternative code block to execute if the if condition is false.
How can you add an element to the end of an array in JavaScript?
push()
What will be the value of x after the following code: let x = 5; x = x + 3;?
8
How do you write an if statement that checks if a number is even?
if (num % 2 === 0) { /* code */ }
How do you access a property of an object in JavaScript?
You can use dot notation or bracket notation, like myObject.propertyName or myObject['propertyName'].
What is the difference between let, const, and var when declaring variables?
let and const have block scope, while var has function scope. const cannot be reassigned after declaration.
What is a ternary operator in JavaScript, and how does it work?
It's a shorthand for an if-else statement. It has the form condition ? expr1 : expr2. If the condition is true, expr1 is evaluated; otherwise, expr2 is evaluated.
Write a for loop that iterates through an array of numbers and calculates their sum.
var sum;
for(var = i; i < array.length; i++){
sum += array[i];
}
What is the value of a variable that has not been initialized?
undefined
Explain the concept of "truthy" and "falsy" values in JavaScript.
In JavaScript, values are inherently "truthy" (e.g., numbers other than 0, non-empty strings, objects) or "falsy" (e.g., 0, empty string, null, undefined, false). These values determine the result of conditional statements.
Explain the difference between an array and an object in JavaScript.
An array is an ordered list of values, accessible by index, typically used for collections of similar items. An object is an unordered collection of key-value pairs, where keys are strings (or Symbols), and values can be of any data type, used for representing structured data