Are there variables in javascript?

Variable means anything that can vary. In JavaScript, a variable stores the data value that can be changed later on.

Use the reserved keyword var to declare a variable in JavaScript.

var ;

var  = ;

A variable must have a unique name. The following declares a variable.

var msg; // declaring a variable without assigning a value

Above, the var msg; is a variable declaration. It does not have any value yet. The default value of variables that do not have any value is undefined.

You can assign a value to a variable using the = operator when you declare it or after the declaration and before accessing it.

var msg; 
msg = "Hello JavaScript!"; // assigned a string value
alert(msg); // access a variable

//the following declares and assign a numeric value
var num = 100; 
var hundred = num;  // assigned a variable to varible

In the above example, the msg variable is declared first and then assigned a string value in the next statement. The num variable is declared and initialized with a numeric value in the same statement. Finally, the hundred variable is declared and initialized with another variable's value.

Multiple Variables Declaration

Multiple variables can also be declared in a single line separated by a comma.

var one = 1, two = 'two', three;

White Spaces and Line Breaks in Variable Declaration

JavaScript allows multiple white spaces and line breaks when you declare a variable with var keyword.

Please note that the semicolon ; at the end is optional.

Loosely-typed Variables

C# or Java has strongly typed variables. It means a variable must be declared with the data type that specifies the type of data a variable will store.

JavaScript is a loosely typed language. It means it does not require a data type to be declared. You can assign any literal values to a variable, e.g., string, integer, float, boolean, etc.

var myvariable = 1;  // numeric value

myvariable = 'one'; // string value

myvariable = 1.1; // decimal value

myvariable = true; // Boolean value

myvariable = null; // null value

Variable Scope

In JavaScript, a variable can be declared either in the global scope or the local scope.

Global Variables

Variables declared out of any function are called global variables. They can be accessed anywhere in the JavaScript code, even inside any function.

Local Variables

Variables declared inside the function are called local variables to that function. They can only be accessed in the function where they are declared but not outside.

The following example includes global and local variables.

var greet = "Hello " // global variable

function myfunction(){
    var msg = "JavaScript!"; 
    alert(greet + msg); //can access global and local variable
}

myfunction();
		
alert(greet);//can access global variable
alert(msg); //error: can't access local variable

Learn global and local scope in JavaScript for more information.

Declare Variables without the var Keyword

Variables can be declared and initialize without the var keyword. However, a value must be assigned to a variable declared without the var keyword.

The variables declared without the var keyword becomes global variables, irrespective of where they are declared. Visit Variable Scope in JavaScript to learn about it.

It is Not Recommended to declare a variable without var keyword because it can accidentally overwrite an existing global variable.

function myfunction(){
    msg = "Hello JavaScript!"; 
}
myfunction();
alert(msg); // msg becomes global variable so can be accessed here

Variable Names in JavaScript

  • Variable names are case-sensitive in JavaScript. So, the variable names msg, MSG, Msg, mSg are considered separate variables.
  • Variable names can contain letters, digits, or the symbols $ and _.
  • A variable name cannot start with a digit 0-9.
  • A variable name cannot be a reserved keyword in JavaScript, e.g. var, function, return cannot be variable names.

  1. Variable stores data value that can be changed later on.
  2. Variables can be defined using var keyword. Variables defined without var keyword become global variables.
  3. Variables must be initialized before accessing it.
  4. JavaScript allows multiple white spaces and line breaks in a variable declaration.
  5. Multiple variables can be defined in a single line separated by a comma.
  6. JavaScript is a loosely-typed language, so a variable can store any type value.
  7. Variable names are case-sensitive.
  8. Variable names can contain letters, digits, or the symbols $ and _. It cannot start with a digit 0 - 9.
  9. Variables can have local or global scope. Local variables cannot be accessed out of the function where they are declared, whereas the global variables can be accessed from anywhere.

Want to check how much you know JavaScript?

Can you have variables in JavaScript?

JavaScript Data Types JavaScript variables can hold numbers like 100 and text values like "John Doe". In programming, text values are called text strings. JavaScript can handle many types of data, but for now, just think of numbers and strings.

What types of variables are there in JavaScript?

In JavaScript, there are three different variable types: var , let , and const . Each of these variables have several rules around how they should be used, and have different characteristics. In this tutorial, we are going to explore the basics of variables in JavaScript.

Is VAR still used in JavaScript?

The var keyword has been around since the inception of JavaScript, and it's what you will see in any pre ES6 code. Variables declared using the var keyword are either globally or functionally scoped, they do not support block-level scope.

Do you have to declare variables in JavaScript?

Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or at a later point in time when you need that variable.