Primitive Data Types

Primitive Data Types

We learned in our last lesson that variables can hold strings and numbers, but what else can they hold?

In this lesson, we're going to explore all of JavaScript's primitive data types. A data type describes what kind of data is being held, and the reason why the following data types are referred to as primitive is because they are considered basic building blocks of the programming language.


Numbers are pretty self-explanatory. After you declare a variable, you can initialize it with a number of any kind, and it will retain that value.

    var cars = 7;

In a later lesson, you will learn how to manipulate these numbers, including how to do basic arithmetic and working with JavaScript's built-in Math library,


Strings in JavaScript are just text that you can assign to a variable. You can concatenate (combine) two strings by using the + operator.

    var city = "New York City";

    console.log("The greatest city in the world is " + city + ".");
    The greatest city in the world is New York City.

Template Literals

An alternate way to work with strings in JavaScript is template literals. Template literals are useful because they support multiple lines and string interpolation (being able to insert variables).

    console.log(`Look how
    cool this string is.
    It is on multiple lines!`);
    Look how
        cool this string is.
        It is on multiple lines!

String Interpolation

String interpolation lets you easily use variables inside your strings via its ${variable} syntax. Here's an example:

    var restaurant = "Chipotle";
    console.log(`My favorite restaurant is ${restaurant}!`);
    My favorite restaurant is Chipotle!

Packaged with strings are some pretty useful and common methods to know. Here are a few of them.


Calling toLowerCase on a string will return the same string with any uppercased letters replaced with lowercase letters.

    var place = "Mexico City";

    mexico city


On the other hand, to do the reverse and capitalize all letters in a string, simply call toUpperCase on it.

    var place = "Mexico City";



There will be times when you will want to replace a certain substring with another substring inside a string.

For example, let's say you want to replace the word red with blue:

    var sentence = "The house is red.";
    var newSentence = sentence.replace("red", "blue");

    The house is blue.


The last JavaScript string method we'll take a look at is the includes method. This method checks to see if a substring exists inside another string, and returns true if it does or false if it does not.

    var sentence = "The house is red.";



A boolean is a data type that has only two values, true or false. True generally means "correct" or "yes" while false generally means "incorrect" or "no".

    var pizzaIsGood = true;
    var wholeFoodsIsCheap = false;


Symbols are tokens that serve as unique IDs. Because each one is different, you can use this to your advantage.

For example, you can use them to give values to constants that you define. Let's say you run a company that makes shirts. You can use symbols to keep track of all the sizes you sell:

    const SIZE_XSMALL = Symbol();
    const SIZE_SMALL  = Symbol();
    const SIZE_MEDIUM = Symbol();
    const SIZE_LARGE  = Symbol();
    const SIZE_XLARGE = Symbol();

In the real world, each size is distinct, and so will each value in these constants. Now with this, you can write elegant code like this:

    function getShirtsLeftInSize(size) {
        if (size === SIZE_XSMALL) {
            return 4;
        } else if (size === SIZE_SMALL) {
            return 6;
        } else if (size === SIZE_MEDIUM) {
            return 3;
        } else if (size === SIZE_LARGE) {
            return 7;
        } else if (size === SIZE_XLARGE) {
            return 12;

    var mySize = SIZE_SMALL;
    console.log("There are " + getShirtsLeftInSize(mySize) + " shifts left in your size.");
    There are 6 shifts left in your size.

Sure, you could do this without using symbols like this:

    const SIZE_XSMALL = "XSMALL";
    const SIZE_SMALL  = "SMALL";
    const SIZE_MEDIUM = "MEDIUM";
    const SIZE_LARGE  = "LARGE";
    const SIZE_XLARGE = "XLARGE";

But the problem with that is that none of the values of these constants can be guaranteed to be unique. Nothing would stop anybody from just creating a new string variable and assigning it a valid size name:

    var mySize = "SMALL";

That would still work, but it requires knowing the exact value of the constant, whereas with symbols, you just need the name of it.


Null is a special value in JavaScript that essentially means nothing.

    var pears = null;

The variable pears is empty and void of a value.



Undefined is another special value in JavaScript. When a variable is declared but not initialized, its value is undefined, because no value was given to that variable.

    var pineapples;


That's pretty much it for JavaScript's primitive data types. There are still more data types, but we'll cover those later in this class.