let greeting;
greetign = {};
console.log(greetign);
- A:
{}
- B:
ReferenceError: greetign is not defined
- C:
undefined
Answer
As the variable named "greeting" doesn't match with the initialized variable "greetign", the variable "greetign" will get assigned as the global scope. Thus, we can see the an empty object in the console. Also, the variable called "greeting" will be declared but won't be use anywhere.
function sum(a, b) {
return a + b;
}
sum(1, "2");
- A:
NaN
- B:
TypeError
- C:
"12"
- D:
3
Answer
For passing a string value "2" with an integer 1 in the sum function, the JavaScript will perform implicit type coercion and convert the number 1 to a string in order to perform the concatenation operation with the string "2".
const food = ["🍕", "🍫", "🥑", "🍔"];
const info = { favoriteFood: food[0] };
info.favoriteFood = "🍝";
console.log(food);
- A:
['🍕', '🍫', '🥑', '🍔']
- B:
['🍝', '🍫', '🥑', '🍔']
- C:
['🍝', '🍕', '🍫', '🥑', '🍔']
- D:
ReferenceError
Answer
As the const variable food was declared with ['🍕', '🍫', '🥑', '🍔'] and it didn't get changed at any part of the code, also the info variable doesn't tried to change/modified the food variable. So, it will log the food variable with it's inital variable.
function sayHi(name) {
return `Hi there, ${name}`;
}
console.log(sayHi());
- A:
Hi there,
- B:
Hi there, undefined
- C:
Hi there, null
- D:
ReferenceError
Answer
In sayHi(name) function, it required a name parameter, but we didn't passed any argument. That's why the parameter will get undefined and it will console log as "Hi there, 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