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 will be the output of the following code snippet?(function(){ setTimeout(()=> console.log(1),2000); console.log(2); setTimeout(()=> console.log(3),0); console.log(4); })();*1 2 3 42 3 4 14 3 2 12 4 3 1
Problem statementSend feedbackWhat will the below code produce on the console?var a;if( typeof(a) ) { console.log("true")}else { console.log("false")}Options: Pick one correct answer from belowtruefalseerror is shownnone of the above
6What will the code below output to the console and why?const obj = {a: 1,b: 2};const arr = [];console.log(arr[0] = { ...obj });Review Later1,2{…obj}{a: 1, b: 2}Does not compile
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.