Lesson Weekend

You've already seen numbers, strings, booleans, and undefined. These are 4 of the 5 basic data types, or primitives, in JavaScript. The other is null, which represents nothingness. Don't worry about null for now - we'll learn more about it down the road.

Data Types


Unsurprisingly, the number type represents numbers.

  • 42
  • 3.14
  • -10
  • 0
  • NaN (stands for "not a number", but is considered a number)
  • Infinity
  • -Infinity
  • var favoriteNumber = 42;

You can use certain methods on numbers.

> 3.14159.toFixed(2);


As you've seen, the string type represents text.

  • "hello, world!"
  • var greeting = "hello, world!";

You can use certain methods on strings.

> "hello".toUpperCase();
> "hello".charAt(2);
> "hello".toUpperCase().charAt(2);


Booleans can only hold 2 possible values - true or false.

> 5 > 3;
> 5 > 10;
> "hello".charAt(2) === "e";
> var enrolledAtEpicodus = true;

You'll discover soon just how important booleans are to programming.


The undefined data type has only one possible value - undefined.

When declaring a variable without giving it a value (var myNumber;), JavaScript creates the variable without a value, hence undefined. Even when declaring a variable and assigning it a value at the same time (var favoriteNumber = 42;), JavaScript actually creates the variable initially without a value, temporarily giving it a value of undefined, before then assigning it the value to the right of the equals sign. This is kind of a subtle point, so don't worry too much about it at the moment.

Additionally, there are some functions and methods that do not return any value, in which case the return value is actually undefined. (You'll learn more about this when we learn how to write our own methods and functions.)

Data Type Detection

It's important to understand the difference between the number 5 and the string "5". To the computer, they are two entirely different things, as illustrated by this example:

> var myNumber = 5;
> var myOtherNumber = 10;
> var myText = "5";
> var myOtherText = "10";
> myNumber + myOtherNumber;
> myText + myOtherText;

When we added 10 to 5 we got 15, but when we added "10" to "5" it concatenated the two strings together.

Likewise, the boolean true is not the same as the string "true".

In the example above, somewhat confusingly, the + operator works on both numbers and strings - just differently. Usually methods will only work on a specific data type. For example, 48432.78.toExponential(); works just fine, but trying to do "48432.78".toExponential(); results in an error because there is no toExponential() method that works on a string. Likewise, "hello".charAt(2); works, but 314159.charAt(2) does not.

We can check the data type of a variable or value as follows:

> typeof 5;
> typeof "5";
> typeof true;
> typeof "true";
> var greeting = "hello world";
> typeof greeting;

Data Type Conversion

Often input from a web browser will come in as a string and we will need to convert it to a number before working with it.

We can convert a string to a number by passing a string to the parseInt() function (more on functions soon):

> var inputtedAge = "45";
> inputtedAge;
> typeof inputtedAge;
> var myAge = parseInt(inputtedAge);
> myAge;
> typeof myAge;

Note that if you try to use parseInt() to convert a string not actually containing a number, the result is the "number" NaN.

> var name = "Andrea";
> var nonsense = parseInt(name);
> nonsense;

If you need to go the other way around, you can convert a number to a string by calling the toString() method on the number:

> var myNumber = 42;
> myNumber;
> typeof myNumber;
> var convertedNumber = myNumber.toString();
> convertedNumber;
> typeof convertedNumber;

Data Types

also known as Primitives

  • Boolean: true or false.

  • Number: Numerical value.

  • String: Content inside of quotes. Such as "This is a string!".

  • Undefined: An object (such as a variable) without a defined value.

  • Null: For now, know that this represents nothingness. We'll explore this more later.


  • parseInt(): Converts a string to a number.

  • toString(): Converts a number to a string.


To check the data type of a variable or value:

> typeof 5;

To convert a string to a number:

> var myNumber = parseInt("42")
> myNumber;

To convert a number to a string:

> var convertedNumber = 42.toString();
> convertedNumber;