Primitive types, and truthy and falsy values

by Bjørn A. Johansen

This first tutorial for beginners is about some basic theory that I believe is important to understand to avoid common JavaScript pitfalls. You might find it a little lengthy or maybe even dry, but trust me, it is worth the effort.

Something to note before we start

If the following seems a bit lengthy for a few definitions, do not let it scare you off, as it is time and words well spend. All to many tutorials, guides and even lectures, jump way too fast over important subjects, in the fear of boring the reader or listener. I do not share that fear, as I believe, that if somebody gets scared away by a few lengthy paragraphs about something important, they are probably not that interested in the subject in the first place, and would surely never have become a good programmer anyway. Programming is about detail and attention to it, and is therefore not for people with a weak attention span and superficial knowledge interests. Programming is for you who want to know why and how, when somebody tells you something.

Regarding the content covered here, I would like to point out, that you do not need to remember every detail of what we discuss before continuing to the next tutorial. This is a slow buildup of your knowledge and confidence in programming JavaScript, so approach this in a relaxed but concentrated manner, and feel assured, that all important material will be walked through many times in many different ways later on, to ensure that you get the best possible understanding of the material, and feel confident using it professionally.

What we are going to look at

In this series named Basic theory, we will focus on some basic JavaScript programming knowledge, and in this tutorial specifically we are going to talk about primitive types, and truthy and falsy values. This is basic but very important stuff, and even if some of it might sound a bit fuzzy at first reading, do not panic and do not skip, it will all help you further on, trust me. This implies that, just because this is basic stuff, it does not mean that it is easy.

Primitive types in JavaScript

So what are primitive types? The primitive types and primitive values (more about that in a bit) are the building blocks of a language, the atoms you might say. In JavaScript all primitive values are members of the following build-in primitive types:

  • Undefined
  • Null
  • Number
  • String
  • Boolean

We are going to start from the top and work our way down, ending with Boolean types which will lead us nicely into truthy and falsy values and their pitfalls.

Undefined

The value undefined has type undefined, and the type undefined represents the primitive value of a variable that has been declared (which is the same as it being initialized) but not yet assigned a value. Now read that again, slowly and let it sink in. This means, that if you declare a variable, which means introducing it into your program by giving it a name (and thereby a space in memory), without assigning it a value (which means storing something in that named memory space) it will get the value undefined. This happens automatically, but of course you can do it manually as well, if you find the need for it. But we will get into that and how exactly to declare a variable and assign it a value, later in this course. If this definition is hard to get your head around, do not worry as we will get back to it and its impact many times later on.

Null

The value null has type null, and the type null represents the intentional absence of any object value. (later in the course, you will see that if you use the typeof operator to check the type of null, you will receive the type Object, which is wrong. This is an implementation error in the language itself, that you will learn to deal with.) This is also worth a second read, and a careful think over. What is meant by this definition is basically that null is an assignment value, that you can assign an object to represent no value. In JavaScript no value is ever set to null automatically, it only happens if you do it in your code.

At this point you might wonder why we write object value and not just value here. The reason for this will become increasingly more clear when we start looking into Objects, but for now lets leave it at, that everything in JavaScript is objects, and therefore anything intentionally left out would by definition be an object value.

Number

The Number type is the set of all possible Number values, which includes the primitive values NaN (Not-a-Number), positive infinity, and negative infinity. We will look a lot more into how to use these last mentioned primitive values later, and for now it is enough for you to have heard about them.

String

The primitive type String is the set of all possible String values. When you deal with websites, you often have to manipulate Strings, so you will see a lot more on them as we move forward. In JavaScript, Strings are 16-bit unsigned integer values, which represents a single 16-bit unit of UTF-16 text. This might not mean much to you, and it will rarely be important for you in your everyday programming, but if it ever comes up, you now know.

Boolean

The primitive type boolean consists of the two, and only these two, primitive values true and false. This I believe, is quite self explanatory, as we all know what it means that something is true or false in a predetermined context. But what is more interesting and less obvious is which values evaluates to true and which evaluate to false. In this respect JavaScript is a tad odd, so it is a good idea to pay extra attention here. Another good reason to concentrate about this, is that many common but hard to find programming bugs stems from a poor understanding of exactly this. So lets take that up as our final endeavor in this tutorial.

Truthy and falsy values

The easiest way to get an overview of what evaluates to what, is to list the falsy values and from that conclude that all other values must be truthy, so here they are;

  • false
  • null
  • undefined
  • "" (empty String)
  • 0 (the number zero)
  • NaN

Everything else in JavaScript, including all objects, evaluates to true. This seems straight forward, with only six values to remember, but be aware that things might not always be what they seem to be. It turns out, that the type of a value is very important when it comes to boolean evaluation, and one thing to be aware of is, that the String "0" evaluates to true, Even though the Number 0 evaluates to false! Another tricky case to be aware of is the String "false" which evaluates to true. These cases are undoubtedly confusing and truly a common pitfall, as we most often only see the value itself and only rarely see its type. To make things even worse, finding the type of a value is not as easy as it should be in JavaScript. Another fact that helps fuel the confusion is that JavaScript is what is called a loosely typed language. This means, that a variable can change not just value but also type at any given time without any notification. To clarify this, lets say that you initialize a variable foo (the name is not important), and assign it the primitive value 0 of type Number. Now there is nothing in the JavaScript language that prevents you from assigning another value to foo of a different type, such as the value "0" of type String. You do not need to know programming to understand the danger of this; the Number 0 evaluates to false while the String "0" evaluates to true, but both are perfectly legal to assign to the same variable doing a program flow, so be aware.

End comment

I hope that you got something out of what we have been talking about, even though I know that it is a lot of information about something you barely have ventured into yet. I also understand that it can make it all seem like a daunting task to take on, but do not worry, definitions are there to be referenced in the beginning, and only remembered with time. If you have made it through this part, you will do just fine in the coming parts.

Thank you and good luck!