Although the JavaScript language doesn't allow you to explicitly define a type when you declare a variable, this shouldn't lead you to believe that JavaScript doesn't have types (because it does).

JavaScript uses dynamic typing, and therefore this means that the JavaScript engine will choose the best data type for your variable based on its value.

So this begs the question, what data types could the JavaScript engine choose for my variable?

Here's a quick summary of the JavaScript Data Types:

Data Type Example
Boolean  
var x = true;
var y = false;

 
Null  
var person = null;

 
Undefined  
var person = undefined;
var variableWithNothingAssigned;

 
Number  
var x = 1.2;
var y = 32;

 
String  
var aString = "What up dude!";

 
Symbol  
const MY_KEY = Symbol();
let obj = {};

obj[MY_KEY] = 123;
console.log(obj[MY_KEY]); // 123

Object  
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

 

Boolean Data Type

The Boolean data type in JavaScript is a primitive type.

It is a pretty straight forward data type as it only contains two valid values (true, false).

Booleans are used when testing conditions. Conditions are found inside of control structures like an if statement (which we'll talk about in the next article), or a while loop.

You're probably already familiar with the concept of Booleans, so let's move onto the next data type.

Null

It's perhaps a bit strange to have null exist as a data type, but nevertheless it does in JavaScript.

Another strange bug inside of JavaScript is that if you inspect the “typeof” null, you'll actually receive Object as the type.

In any case, in an attempt to stop confusing you, just know that null is used to represent “nothing”. It's typically used as a place holder for an object.

Undefined

Not to be confused with null, the undefined data type in JavaScript is used to tell the coder that a variable has been declared but hasn't yet been assigned any value.

So when we write the code var aVariable;, the resulting data type for aVariable would be undefined as we haven't yet assigned a value.

What you could do if you wanted aVariable to no longer be assigned as an undefined data type, but still didn't want to assign anything truly meaningful, you could assign it the null type.

Number

What I personally enjoy about JavaScript is that it only has one data type to specify a number, and that's just the Number data type.

When you think about the Number data type in JavaScript as it relates to the Java language, you can state that a Number in JavaScript can be an Integer, a Double, a Float, or a BigDecimal all at the same time. So you don't need to worry about the crazy details about how JavaScript is going to handle your numbers!

One helpful function in JavaScript that relates to the Number data type is the isNaN() function.

This function is used to determine if a value is “Not-a-Number”. It's a bit confusing as you're working on things in the “negative”, so if you want to check if something is a number you're looking for a boolean value of false when running the isNaN() function.

String

A String is likely the most commonly used data type across any modern programming language.

A String is a data type that represents any combination of characters, special character and/or numbers that are assigned to a variable. Typically, Strings are used to store English language sentences, names, or even numbers!

You can think of Strings as a sort of “catch-all” for all the data types. What I mean by this is that if the JavaScript engine can't figure out what data type to assign your variable, it's probably going to just assign the String data type to your variable.

Strings also have a wide variety of functions and properties that make them extremely versatile and useful in the JavaScript language.

We'll be diving into details in later articles with respect to all the things that you can do with Strings.

Symbol

The Symbol data type is a new addition to the JavaScript language.

It's unique and immutable. It's unique because any Symbol that is created in your code is guaranteed to be different from any other Symbol you create. It's immutable because you will not be able to change the value of any Symbol after it has been created.

Symbols are typically used as keys for properties due to the fact that they are unique and immutable.

If you want to read more about Symbols, I found a great article here.

Object

Probably the most powerful and flexible data type in JavaScript, the Object data type is the only type in JavaScript that isn't a primitive type.

Since the JavaScript language is Object oriented, the Object data type exists to fulfill its obligation.

What's neat is that you can create an Object and assign properties to it in two different ways.

The first is to create properties in an Object by using dot notation like so:

var person = {}; // note that we use the open/close curly braces to declare a variable as an Object type
person.name = "Trevor Page";
person.age = 32;
person.gender = "Male";

This will create an Object variable called person with three properties that you'll be able to access like so:

console.log(person.name); // outputs "Trevor Page"
console.log(person.age); // outputs 32;
console.log(person.gender); // outputs "Male";

Alternatively you can also use something called JSON (which is an acronym for JavaScript Object Notation) to create an Object in JavaScript.

The equivalent person Object can be created with JSON like so:

var person = {name: "Trevor Page", age: 32, gender: "Male"};

Note: There are subtle differences between the two person objects that get created by the JavaScript engine, but these differences aren't worth mentioning now as they are a more advanced topic.

Free Java Beginners Course

Start learning how to code today with our free beginners course. Learn more.