How to Declare a Variable in JavaScript

If you’re just getting started with JavaScript, you may be wondering how to declare a variable. Luckily, it’s pretty easy! In this blog post, we’ll show you how to declare a variable in JavaScript, as well as some best practices to keep in mind.

Checkout this video:


JavaScript variables are containers for storing data values.

In other words, a variable is simply a box that you can store stuff in. You can use variables to store numbers, strings, objects, and even functions.

Variables are one of the most important concepts in programming. Once you understand how to create and use variables, you’ll be on your way to becoming a JavaScript pro!

What is a Variable?

In programming, a variable is a value that can be changed, depending on conditions or on user input. In other words, a variable is like a container that can hold a value that can be changed.

For example, in the following code, the variable x is assigned the value 5. But later in the code, the value of x is changed to 10. So when the code is run, first x will have the value 5, and then it will have the value 10.

var x = 5;
x = 10;

This concept might be confusing at first, but you’ll get used to it with practice. Just remember that a variable is like a container that can hold a value which can be changed.

Why Do We Need Variables?

A variable is a “named storage” for data. We can use variables to store data like numbers or strings. Later, we can retrieve the data stored in the variable and use it in our program.

In JavaScript, we use the keyword “var” to create a variable. For example,

var myName = “John”;
This line creates a new variable called myName and stores the string value “John” in it. From now on, we can use the myName variable anywhere in our program to refer to the string “John”.

We can also create variables that store numbers like this:

var myAge = 20;
This line creates a new variable called myAge and stores the number value 20 in it.

Declaring a Variable

In JavaScript, declaring a variable is simply a matter of assigning it a value with the equals (=) operator. For example:

var answer = 42;
This statement creates a variable called answer and assigns it the value 42. You can call your variables anything you like, as long as they conform to the rules described below.

You can declare several variables in one go by comma-separating them:

var a = 1, b = 2, c = 3;
This will create three variables with the values 1, 2 and 3.

Assigning a Value to a Variable

Whenever you want to use a value in your program, you have to first declare a variable and then assign a value to that variable. The value can be of any data type like a number, string, or an array. You can also declare multiple variables of the same type in one statement by using commas to separate the variable names.

Here is the syntax for assigning a value to a variable in JavaScript:

var variablename = value;

For example:

var x = 42;
var y = “Hello”;

Rules for Naming Variables

When creating a variable in JavaScript, there are a few rules and conventions to follow. These rules will help you choose names that are easier to read and understand, and that won’t accidentally interfere with the rest of your code.

1. Variable names can be made up of letters, numbers, dollar signs, and underscores.

2. Variable names cannot start with a number.
3. Variable names are case sensitive, so myVariable is different from MyVariable.
4. Avoid using JavaScript reserved words like function and var as variable names.

Here are some examples of valid variable names:
var name;
var firstName; // use camel case when naming variables made up of multiple words
var $money; // dollars signs are often used in front of variables that hold money values
var _secret; // underscore can be used to indicate a “private” variable
Now that we’ve covered the rules for naming variables, let’s look at how to actually declare them in JavaScript.


In conclusion, variables are a crucial part of programming in any language. They allow you to store data and manipulate it as needed. In JavaScript, there are several ways to declare variables, but the most common is using the var keyword. When you use var, you can optionally specify a type (such as number or string) but it is not required. After the variable is declared, you can assign a value to it using the = operator. Once a value is assigned, you can access the variable by name.

Scroll to Top