Skip to content

Commit 1f34a40

Browse files
committed
adding tricky questions
1 parent e2a174a commit 1f34a40

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Loading

Javascript/Tricky-JS-Problems/what-below-code-will-output-5.md

+19
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,22 @@ function display() {
4242
var a = b;
4343
}
4444
```
45+
46+
So b becomes a global variable because there is no var keyword before it and a becomes a local variable. Therefore, outside the function, only b is available so `typeof a === 'undefined'` comes as true and `typeof b === 'undefined'` comes as false.
47+
48+
![](2020-10-08-17-22-29.png)
49+
50+
If we execute the above code in strict mode as shown below,
51+
52+
```js
53+
"use strict"
54+
function display() {
55+
var a = (b = 10)
56+
}
57+
display()
58+
console.log("b", typeof b === "undefined")
59+
console.log("a", typeof a === "undefined")
60+
```
61+
62+
It will throw an error because b becomes a global variable and strict mode does not allow creating global variables so you will get an error when you execute this code.
63+
That’s it for today. I hope you learned something new.

0 commit comments

Comments
 (0)