avatarXiaoyun Yang

Summary

The web content provides tips for effectively navigating JavaScript's loosely typed system, emphasizing the use of typeof, the difference between double and triple equals, and how to check for falsy values.

Abstract

JavaScript's flexible type system can lead to debugging challenges due to its loose typing, which automatically assigns variable types based on the assigned value. The article offers three main tips to mitigate these issues: first, the typeof operator can be used to determine the type of a variable when it's unclear; second, the use of triple equals (===) is recommended for strict equality checks to prevent type coercion; and third, the "bang" operator (!) can help identify falsy values such as null, undefined, 0, "", and false. The article also advises against adding numbers to null due to the potential for unexpected results, and it highlights the importance of understanding the nuances between different falsy values and their types.

Opinions

  • The author suggests that while JavaScript's type system offers flexibility, it can also be a source of bugs due to the lack of strict typing.
  • The use of === is strongly recommended over == to ensure type safety and prevent subtle bugs.
  • The article conveys that understanding the concept of truthiness and falsiness in JavaScript is crucial for writing robust code.
  • It is implied that checking for all falsy values individually can be tedious, and using the ! operator is a more efficient approach.
  • The author warns against the practice of adding numbers to null, as it can lead to non-intuitive outcomes and should be avoided for best practices.

3 Tips For JavaScript’s Type System

JavaScript is a loosely typed language, meaning you don’t have to specify what type of information will be stored in a variable in advance. JavaScript automatically types a variable based on what kind of information you assign to it (e.g., that '' or " " to indicate string values). Many other languages, like Java, require you to declare a variable’s type, such as int, float, boolean, or String.

This is both a blessing and a curse.

Photo by Alex on Unsplash

While JavaScript’s type system gives you a lot of flexibility, it lacks the capability of a strongly typed system to yell at you when you are trying to add an int to an object, which protects you from spending hours debugging a type error.

Here are some useful tips to help you navigate JavaScript’s weird type system.

#1 typeof

whenever a variable’s type is in doubt, you can employ the typeof operator:

typeof 1 //> “number”
typeof1//> “string”
typeof [1,2,3] //> “object”
typeof {name: “john”, country: “usa”} //> “object”

typeof true //> “boolean”
typeof (1 === 1) //> “boolean”
typeof undefined //> “undefined”
typeof null //> “object”
const f = () => 2
typeof f //> “function”

Key Takeaway: When the type is in doubt, use typeof.

#2 Double Equals Versus Triple Equals

The === mean “equality without type coercion”. Using the triple equals, the values must be equal in type as well.

1 == “1” //> true
1 === “1” //> false
null == undefined //> true
null === undefined //> false
"0" == false //> true
"0" === false //> false
0 == false //> true
0 === false //> false, because they are of a different type

Note, you can use !== for checking inequality with type coercion:

1 != "1" //> false
1 !== "1" //> true

Key Takeaway: Always use === since that is a more thorough check and help you avoid nasty bugs.

#3 Checking for Falsiness

Using the ! operator (called the “bang” operator) to check for falsiness, we notice the following:

![] //> false
!42 //> false
!"hello world" //> false
!null //> true
!undefined //> true
!0 //> true
!"" //> true
!false //> true

null, undefined, 0, “”, and false all returned true when we applied the ! operator. When we applied the ! operator on things that do exist, such as the number 42 or the string “hello world”, we get false.

This shows that null, undefined, 0, “”, and false all represent non-existence or falsy values, but not in the same way. 0 is a number and represents the non-existence of quantity. “” is a string and represents the non-existence of substance in the string. And lastly, false is a boolean and represents the lack of true. This can be verified with a simple typeof:

typeof 0 //> "number"
typeof "" //> "string"
typeof false //> "boolean"

undefined and null are more tricky. As shown by the example below, they are different types, have the same value, but fails the === comparison:

typeof undefined //> “undefined”
typeof null //> “object”
undefined == null //> true
undefined === null //> false

Because null is an object, we can add a number to it. For undefined however, adding a number to it gives you NaN.

null + //> 1
undefined + 1 //> NaN

Adding a number to a null seems like it could be a nice shortcut sometimes, but don’t do it!

Although null is an object, you get a number when you add a number to it. However, when you add other things to it, it gives you non-sensical strings:

null + [1,2,3]//> "null1,2,3"
null + ["hello", "world"] //> "nullhello,world"
null + {0: "hello", 1: "world"} //> "null[object Object]"

Key Takeaways:

  • You need to be careful when you check for truthiness. Use === and check of all types of potential falsy values, e.g., if(foo === undefined || foo === null) ... — which can get quite tedious.
  • Don’t add things to null.
  • Best practice is to check for falsiness instead by using the ! operator to check for all of the falsy values, i.e., null, undefined, 0, “”, and false, in one action.

Thanks for reading!

If you think this article was helpful, please check out the other articles and tutorials I’ve written on JavaScript:

JavaScript
Type
ES6
Hacks
Programming
Recommended from ReadMedium