Read each JavaScript program and choose what it prints. Every program was run on Node.js as a normal (non-strict) script unless it says "use strict", so the answers are the real output.
Question 11Easy
What does the following JavaScript code print (console output, as in Node.js)?
main.js
let total = 10;
let Total = 20;
let TOTAL = total + Total;
console.log(total, Total, TOTAL);
A10 20 30
B20 20 40
CSyntaxError: total has already been declared
D30 30 30
Correct Answer: Option A
Identifiers are case-sensitive, so total, Total and TOTAL are three separate variables.
What does the following JavaScript code print (console output, as in Node.js)?
main.js
function kind() {
return typeof this;
}
console.log(kind(), this === undefined);
Aundefined true
Bobject true
Cobject false
Dfunction false
Correct Answer: Option C
Non-strict plain calls receive the global object as this, so typeof this is "object". At the top level of a Node.js script this is module.exports ({}), which is not undefined either.
What does the following JavaScript code print (console output, as in Node.js)?
main.js
function setScore() {
score = 90; // no let, const or var
}
setScore();
console.log(score);
AReferenceError: score is not defined
Bundefined
C90
DSyntaxError
Correct Answer: Option C
In non-strict code, assigning to an undeclared name creates a property on the global object, so score is visible outside the function. In strict mode the same code throws ReferenceError.
console.log inspects arrays and objects (strings shown in single quotes). "1,two,true" and [object Object] are what String() gives, and the JSON form comes from JSON.stringify().
All numbers are 64-bit floating point, so 10 / 4 is 2.5 (no integer division). Dividing a non-zero number by 0 gives Infinity or -Infinity; only 0 / 0 is NaN.
What does the following JavaScript code print (console output, as in Node.js)?
main.js
{
let inner = 1;
}
console.log(typeof inner);
Aundefined
Bnumber
CReferenceError: inner is not defined
Dobject
Correct Answer: Option A
let is block-scoped, so inner does not exist outside the braces. typeof is the one operator that does not throw for an undeclared name – it returns "undefined".
What does the following JavaScript code print (console output, as in Node.js)?
main.js
greet("Asha");
function greet(name) {
console.log(`Hello, ${name}`);
}
AReferenceError: greet is not defined
BTypeError: greet is not a function
Cundefined
DHello, Asha
Correct Answer: Option D
Function declarations are hoisted completely – name and body – so they can be called before the line where they appear. (Function expressions assigned to let/const are not.)
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·