Javascript Types

Javascript has a lot of typical types, which are common across languages. Javascript is weakly typed, meaning types are not directly defined in the code. Instead, Javascript decides on the types of things based on their context in the code.

Javascript has six primitives:

  • undefined - when something is not defined in the code, or does not exist.
  • Number - a number i.e. 1.
  • String - a combination of characters i.e. test.
  • Boolean - true or false.
  • BigInt - a number bigger than 253 - 1.
  • Symbol - a completely unique identifier.

As well as these, there are function and object types.

How do we check types?

We can check what type a variable is by using the typeof keyword. typeof returns the string name of the type, as shown below:

javascript Copy
let i = 0; if(typeof i === "number") { // Will run this code, since i is of type "number" }

Let's look at some examples of our types, now that we know about this keyword:

javascript Copy
typeof undefined; // Returns "undefined" typeof 5; // Returns "number" typeof "hello"; // Returns "string" typeof true; // Returns "boolean" typeof BigInt(10000000000000000); // Returns "BigInt" typeof Symbol("Hi"); // Returns "symbol" typeof {}; // Returns "object" typeof function(){} // Returns "function" typeof null // Null is strangely an "object" type

Truthy and Falsy

Since Javascript types are quite confusing, we have the concept of truthy and falsy.

falsy can refer to any of the following values:

javascript Copy
// Falsy values in Javascript NaN // NaN is falsy 0 // 0 is falsy -0 // -0 is falsy undefined // undefined is falsy null // null is falsy "" // Empty strings are falsy '' // Empty strings are falsy `` // Empty strings are falsy document.all // document.all is the only falsy object false // false is of course falsy

truthy then refers to anything which is not falsy. These two concepts are important in Javascript types since there are a number of operators which allow us to change variables based on this information.

Truthy and Falsy Operators

Truthy AND operator

The truthy AND operator lets you change the value of something if the value is truthy. That means you can do something like this:

javascript Copy
let i = 5 && 6;

Since 5 is truthy, i actually returns a value of 6. Think of the text after the && as a backup variable. If the first value is truthy, then we should use the second one. Otherwise, we'll use the first value if it is falsy.

Falsy Nullish operator

Similarly, we can use the nullish operator, to check if something is falsy. If the first value is falsy, then we will use the value after ??.

javascript Copy
let j = 4 ?? 6;

Logical OR Operator

To confuse you even further, there is something that is slightly different from the nullish coalescing operator, but also very similar. The logical OR operator will return the second value if the first one is false.

Expressions that can be converted to false include 0, null, NaN, an empty string (i.e. ""), or undefined.

In the below example, the number after the || operator is the value returned for j.

javascript Copy
let i = undefined || 5; // since the first value can be converted to false, the value of i is 5.
Last Updated Friday, 18 June 2021


Subscribe to stay up to date with our latest posts via email. You can opt out at any time.

Not a valid email