let greeting;
greetign = {};
console.log(greetign);
- A:
{}
- B:
ReferenceError: greetign is not defined
- C:
undefined
Answer
Declared the variable as greeting but then tried to assign an empty object to a variable named greetign. JavaScript is case-sensitive, so greeting and greetign are considered different variables.so say that greetign is not defined
function sum(a, b) {
return a + b;
}
sum(1, "2");
- A:
NaN
- B:
TypeError
- C:
"12"
- D:
3
Answer
the sum function takes two parameters a and b and attempts to add them together. When you call sum(1, "2"), the first parameter 1 is a number, but the second parameter "2" is a string.In JavaScript, when you use the + operator with a string and a number, the number is coerced into a string, and then the two strings are concatenated.
const food = ["🍕", "🍫", "🥑", "🍔"];
const info = { favoriteFood: food[0] };
info.favoriteFood = "🍝";
console.log(food);
- A:
['🍕', '🍫', '🥑', '🍔']
- B:
['🍝', '🍫', '🥑', '🍔']
- C:
['🍝', '🍕', '🍫', '🥑', '🍔']
- D:
ReferenceError
Answer
The info.favoriteFood property is assigned a new value "🍝", but it is not change the food array itself. The array remains unchanged, and its elements are still ["🍕", "🍫", "🥑", "🍔"].
function sayHi(name) {
return `Hi there, ${name}`;
}
console.log(sayHi());
- A:
Hi there,
- B:
Hi there, undefined
- C:
Hi there, null
- D:
ReferenceError
Answer
the sayHi function expects a name parameter, but when we call the function in the console.log statement, and do not pass any argument. In JavaScript, if a function parameter is not provided with a value, it will default to undefined.
let count = 0;
const nums = [0, 1, 2, 3];
nums.forEach((num) => {
if (num) count += 1;
});
console.log(count);
- A: 1
- B: 2
- C: 3
- D: 4
Answer
the forEach method iterates over the nums array. Inside the callback function, the code checks if the current num is truthy , and if so, it increments the count variable by 1.