Lesson 3 of 16
In Progress

Variables and Data Types

In JavaScript, a variable is a named container that holds a value. Variables are used to store and manipulate data in your code.

In this article, we’ll go over the basics of variables and data types in JavaScript, including how to declare variables, assign values, and work with different data types.

Declaring Variables

In JavaScript, you declare a variable using the var keyword followed by the variable name. Here’s an example:

var message;

This declares a variable named message that currently has no value. You can also declare a variable and assign it a value at the same time using the assignment operator (=):

var message = "Hello, world!";

Data Types

In JavaScript, there are several data types that you can use to store different kinds of values. Here are the most common data types in JavaScript:


A string is a sequence of characters, such as words or phrases. Strings are declared using single or double quotes:

var message = "Hello, world!";
var name = 'John';


A number is a numeric value. In JavaScript, there is only one type of number, which can be either an integer or a floating-point number. There is no separate data type for integers or decimals.

var age = 25;
var price = 19.99;


A boolean is a value that is either true or false. Booleans are often used in control structures like if statements to test for certain conditions.

var isValid = true;
var isEmpty = false;


The null data type represents a value that is intentionally empty or non-existent. It is different from an undefined value, which means that a variable has been declared but has not been assigned a value.

var user = null;


The undefined data type represents a variable that has been declared but has not been assigned a value.

var user;

Type Coercion

In JavaScript, you can use variables to store values of different data types. For example, you can store a string in a variable and then later reassign it to a number:

var value = "10"; 
value = 10;

This is called type coercion, and it allows you to store different data types in the same variable. However, it can also lead to potential issues if you are not careful, as you may end up with unexpected results when performing operations on variables with different data types.


Variables and data types are essential concepts in JavaScript programming. By understanding how to declare variables, assign values, and work with different data types, you’ll have a solid foundation for building interactive and dynamic web applications with JavaScript.


To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Declare a variable and assign it a string value.

var message = "Hello, world!";

Declare a variable and assign it an integer.

var age = 25;

Declare a variable and assign it a boolean value.

var isValid = true;

Declare a variable and assign it a null value.

var user = null;

Assign a string value to a variable and then reassign it to a number value.

var value = "10";
value = 10;