lydiahallie /javascript-questions

A long list of (advanced) JavaScript questions, and their explanations :sparkles:

★ 65,275⑂ 9,200👁 1,254⑃ 555● 254▤ 0◈ 0
MITCreated 2019-06-12
65,275
stars
+-2+4+-17
9,200
forks
1,254
watchers
555
prs
254
issues
0
releases
0
discussions

README

JavaScript Questions


 [!NOTE]  
 This repo was created in 2019 and the questions provided here are therefore based on the JavaScript syntax and behavior at that time. Since JavaScript is a constantly evolving language, there are newer language features that are not covered by the questions here.

---


From basic to advanced: test how well you know JavaScript, refresh your knowledge a bit or prepare for your coding interview! :muscle: :rocket: I update this repo regularly with new questions. I added the answers in the collapsed sections below the questions, simply click on them to expand it. It's just for fun, good luck! :heart:

Feel free to reach out to me! 😊


  Instagram || Twitter || LinkedIn || Blog


| Feel free to use them in a project! 😃 I would _really_ appreciate a reference to this repo, I create the questions and explanations (yes I'm sad lol) and the community helps me so much to maintain and improve it! 💪🏼 Thank you and have fun! |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |

 See 20 Available Translations 🇸🇦🇪🇬🇧🇦🇩🇪🇪🇸🇫🇷🇮🇩🇯🇵🇰🇷🇳🇱🇧🇷🇷🇺🇹🇭🇹🇷🇺🇦🇻🇳🇨🇳🇹🇼🇽🇰


- 🇸🇦 العربية
- 🇪🇬 اللغة العامية
- 🇧🇦 Bosanski
- 🇩🇪 Deutsch
- 🇪🇸 Español
- 🇫🇷 Français
- 🇮🇩 Indonesia
- 🇮🇹 Italiano
- 🇯🇵 日本語
- 🇰🇷 한국어
- 🇳🇱 Nederlands
- 🇵🇱 Polski
- 🇧🇷 Português Brasil
- 🇷o Română
- 🇷🇺 Русский
- 🇽🇰 Shqip
- 🇹🇭 ไทย
- 🇹🇷 Türkçe
- 🇺🇦 Українська мова
- 🇻🇳 Tiếng Việt
- 🇨🇳 简体中文
- 🇹🇼 繁體中文




---

 1. What's the output?

javascript
function sayHi() {
  console.log(name);
  console.log(age);
  var name = 'Lydia';
  let age = 21;
}

sayHi();


- A: Lydia and undefined
- B: Lydia and ReferenceError
- C: ReferenceError and 21
- D: undefined and ReferenceError

Answer


 Answer: D

Within the function, we first declare the name variable with the var keyword. This means that the variable gets hoisted (memory space is set up during the creation phase) with the default value of undefined, until we actually get to the line where we define the variable. We haven't defined the variable yet on the line where we try to log the name variable, so it still holds the value of undefined.

Variables with the let keyword (and const) are hoisted, but unlike var, don't get initialized. They are not accessible before the line we declare (initialize) them. This is called the "temporal dead zone". When we try to access the variables before they are declared, JavaScript throws a ReferenceError.




---

 2. What's the output?

javascript
for (var i = 0; i  console.log(i), 1);
}

for (let i = 0; i  console.log(i), 1);
}


- A: 0 1 2 and 0 1 2
- B: 0 1 2 and 3 3 3
- C: 3 3 3 and 0 1 2

Answer


 Answer: C

Because of the event queue in JavaScript, the setTimeout callback function is called _after_ the loop has been executed. Since the variable i in the first loop was declared using the var keyword, this value w

Top issues

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON