What will the below statements print on the console?"use strict"function abc() {console.log(this);}abc();
Question
What will the below statements print on the console?
"use strict";
function abc() {
console.log(this);
}
abc();
Solution
The output of the code will be undefined.
Here's the step by step explanation:
-
"use strict" is a directive introduced in ECMAScript 5. It helps in catching common coding mistakes and "unsafe" actions such as using undeclared variables. When we use "use strict", the value of this in a function is undefined if the function is called in the global context.
-
The function abc() is defined, which logs the value of this to the console.
-
The function abc() is called in the global context.
-
Since we are in strict mode and the function is called in the global context, the value of this inside the function is undefined.
-
Therefore, undefined is logged to the console.
Similar Questions
What is the output of following code?var a = 10;function test() { a = 20;}test();console.log(a);Options: Pick one correct answer from below1020
What is the output of this code?const a = 12;function myFunction(a) { console.log(a);}myFunction(89);112289
What is the output of this code?function myFunction(a) { console.log(a + b);}const b = 79;myFunction(10);
Which statement invokes the function? Function definition Function call Function header All
How do Javascript primitive/object types passed in functions? By Value By Reference None of the above Both A and B
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.