YOU DON'T KNOW JS: JAVASCRIPT GRAMMER PART1 TL; DR

By: Saurav

2018-02-04 06:54:00 UTC

1. Javascript has seven built-in types: null, undefined, boolean, number, string, object, symbol
2. They can be identified using typeof operator
3. Variables don't have types, the value stored in them do. So when you do something like:

var a = 4;
typeof a //number

You are actually asking for the type of value stored in the variable a

4. "undefined" and "undeclared" are very different. undefined is a value that a declared variable can hold. "Undeclared" means a variable has never been declared.

5. Javascript, unfortunately, kind of conflates these two terms, not only in its error messages ("ReferenceError: a is not defined", should have been not declared) but also in the return values of typeof, which is "undefined" for both cases.

6. The safety guard (preventing an error) on typeof, when used against an undeclared variable, can be helpful in certain cases as shown below:

// oops, this would throw an error!
if (DEBUG) {
	console.log( "Debugging is starting" );
}

// this is a safe existence check
if (typeof DEBUG !== "undefined") {
	console.log( "Debugging is starting" );
}

Note: This TL;DR is a note from You Don't know JS. Personally, I find this book the best for understanding core javascript. Go read more here: YDKJS

Owned & Maintained by Saurav Prakash

If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)