JavaScript question #Day 9

What’s the output ?

function getAge() {
‘use strict’;
age = 21;
console.log(age);
}

getAge();

A: 21

B: undefined

C: ReferenceError

D: TypeError

Answer: C

With “use strict”, you can make sure that you don’t accidentally declare g…


This content originally appeared on DEV Community and was authored by Sooraj S

What's the output ?

function getAge() {
  'use strict';
  age = 21;
  console.log(age);
}

getAge();
  • A: 21
  • B: undefined
  • C: ReferenceError
  • D: TypeError

Answer: C

With "use strict", you can make sure that you don't accidentally declare global variables. We never declared the variable age, and since we use "use strict", it will throw a reference error. If we didn't use "use strict", it would have worked, since the property age would have gotten added to the global object.


This content originally appeared on DEV Community and was authored by Sooraj S


Print Share Comment Cite Upload Translate Updates
APA

Sooraj S | Sciencx (2021-07-19T13:58:32+00:00) JavaScript question #Day 9. Retrieved from https://www.scien.cx/2021/07/19/javascript-question-day-9/

MLA
" » JavaScript question #Day 9." Sooraj S | Sciencx - Monday July 19, 2021, https://www.scien.cx/2021/07/19/javascript-question-day-9/
HARVARD
Sooraj S | Sciencx Monday July 19, 2021 » JavaScript question #Day 9., viewed ,<https://www.scien.cx/2021/07/19/javascript-question-day-9/>
VANCOUVER
Sooraj S | Sciencx - » JavaScript question #Day 9. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/19/javascript-question-day-9/
CHICAGO
" » JavaScript question #Day 9." Sooraj S | Sciencx - Accessed . https://www.scien.cx/2021/07/19/javascript-question-day-9/
IEEE
" » JavaScript question #Day 9." Sooraj S | Sciencx [Online]. Available: https://www.scien.cx/2021/07/19/javascript-question-day-9/. [Accessed: ]
rf:citation
» JavaScript question #Day 9 | Sooraj S | Sciencx | https://www.scien.cx/2021/07/19/javascript-question-day-9/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.