

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Learn JavaScript
incomplete
2: Basic Variables
incomplete
3: let and const
incomplete
4: Why JavaScript?
incomplete
5: Comments
incomplete
6: Numbers in JS
incomplete
7: Numbers Review
incomplete
8: Increment and Decrement
incomplete
9: Undefined vs. Undeclared
incomplete
10: Null vs. Undefined
incomplete
11: Dynamic and Weak
incomplete
12: Same Line Declarations
incomplete
13: JavaScript's Speed
incomplete
14: Strings
incomplete
15: Template Literals
incomplete
16: Java vs. JavaScript
incomplete
17: Semi-Colons in JS
incomplete
18: String Encoding
incomplete
19: Camel Case in JS
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
If you're coming from Python, you might be thinking:
Ah, so
undefinedis likeNone! Easy peasy.
Yes. But also... no. One of JavaScript's most cursed features is that it has two values for "nothing":
undefined: It doesn't exist at all. In grug-speak undefined is "very nothing"null: It (kind of) exists, but it's empty. In grug-speak null is "kinda nothing"There are some practical differences between the two, the primary one being that undefined is the default value of a variable when it hasn't been given a value yet.
let myName;
console.log(myName); // undefined
To get a null value, you have to explicitly assign it:
let myName = null;
console.log(myName); // null
Confusingly, typeof returns "object" for null:
console.log(typeof null); // object
To be clear, null is its own type according to the ECMAScript specification, but the "object" type report is a historical quirk that can't be easily fixed now.
In most cases, null and undefined work the same way, but you'll want to be consistent in how you use them, and know that there are subtle behavioral differences.
I personally use undefined almost everywhere I would use None in Python or nil in Go. JavaScript is fairly unique in having two options. I only use null in cases where the behavioral difference matters, or I'm relying on external code that forces me to use null.
Textio has lots of legacy code that checks if specific values are null. Just to be safe, the engineering team decided on a standard where variables that aren't declared with a specific value should be set to null instead of the default undefined. Fix the code so that each variable holds a null value.