JavaScript Variable – Naming, Declaration, Initialization with Example

Since we have studied JavaScript Classes. Today, we will learn JavaScript Variables. Basically, variable means anything that can vary and holds the data value and we can change it anytime. A variable must have a unique name. Here, we will explore the declaration and initialization process of variables. Along with this, we will see how to name the variable with some reserved words and variable scopes. For better understanding, we will see some JavaScript variable examples to get a clear picture.

Learn JavaScript Variables with Example

What is JavaScript Variable?

A JavaScript variable will get assigned to a memory location, all the operations done on the variable, effects that memory location. Its value gets changed during program execution. A variables declaration is necessary before using it.

We can declare a variable in JavaScript, using keyword var followed by the name of the variable and semicolon.

The syntax to create a variable in JavaScript:

var var_name;

The var_name is the name of the variable which should be defined by the user and should be unique. These types of names are also known as Identifiers. The following are the guideline for naming the identifier:

  • It must be a letter, an underscore(_) or a dollarsign($).
  • We can initialize the variables either at the time of declarations or also later when we want to use them.

Recommended Reading – JavaScript Syntax Structure and Implementation

Example: For declaring and initializing variables in JavaScript.

// declaring single variable
var name;

//declaring multiple variable
Var name, title, num;

// initialising variables
var name = “Harsh”;
var = “Rakesh”;

With this example, we learn:

// creating variable to store a number
var num = 5;
// store string in the variable num
num = “Warren”;

Example:

var <variable_name>;
var <variable_name> = <value>;

How to Declare and Initialize JavaScript Variable?

var one = 1; // variable stores numeric value
var two = ‘two’; //variable stores string value

With a var keyword, we can declare any variable. Whereas, in the example variable three is declared but does not hold any value yet, so its value will be ‘undefined’;

How can we declare JavaScript variable in a single line?

We can declare multiple variables in a single line with the help of comma.

Example:

var one =1, two = ‘two’, three;

Var Hoisting

  • To execute any code, we have to declare a variable first. This also means that a variable can appear to be used before its declared. The “Hoisting”, means that the variable declaration move to the top of the function or global code.

Example: 

bla = 2;
var bla;
// …
// is implicitly understood as
var bla;
bla =2;

It is always recommended to declare a variable at the top of scope (at the top of the global code and at the top of the function code). So, it’s clear which variables are functioned scope(local) and which are resolved on the scope chain.

  • Hoisting will affect the variables declaration, but not its value’s initialization.

Example:

function do_something() {
  Console.log(bar); //undefined
  var bar = 111;
 console.log(bar);
}
// is implicitly understood as

function do_something() {
   var bar;
   console.log(bar);
   bar = 111;
   console.log(bar);
}
  • CamelCase is used for the multiple names. That is, words go one after another, each starting with a capital letter:

Example:

myVeryLongName
  • These variables are the case – sensitive.

Variable apple and APPLE are two different variables.

  • We can use non-English letters for variable naming but they do not recommend.

Naming the JavaScript Variable

While naming your variable in JavaScript, you should follow the following rules:

  • We cannot use the reserved keywords for naming the JavaScript variable. For Ex: break or Boolean names are not valid.
  • The variables should not start with numeric values. They must begin with a letter. 88test is an invalid variable name but _88test is a valid one.
  • The variables names are case sensitive. For Ex: Name and name are two different variables.

Reserved Words for JavaScript Variable

We can not use the reserve names as a variable name, because the language uses them itself.

Example: let let =5; // const name a variable ‘let’ error.

AbstractElseInstanceofSwitch
BooleanEnumIntSynchronized
BreakExportInterfaceThis
ByteExtendsLongThrow
CaseFALSENativeThrows
CatchFinalNewTransient
CharFinallyNullTRUE
ClassFloatPackageTry

You must know 10 Major Applications of JavaScript 

An Assignment without “use strict”

You should know what are variables in JavaScript before using it. But, earlier it was technically possible to create a variable by a mere assignment of the value without using let. This still works now if we don’t put use strictly in our scripts to maintain compatibility.

Example:

// no “use strict” in the example
  num = 5;
  alert(num);
“use strict”

JavaScript Variable Scope

There are two types of variable scope in JavaScript:

  • JavaScript Global Variables – We can define this variable anywhere in the code.
  • JavaScript  Local Variables – A local variable will be visible only written a function where it is defined. Function parameters are always local to that function.

If you declare a local variable or functions parameter with the same name as a global variable, you effectively hide the global variable.

<html>
<body>
  <script type = “text/javascript”>
    <! – 
         var myVar = “global”;
         var myVar = “local”;
         document.write(myVar);
     // -- >
   </script>
</body>
</html>

Summary

So, we have learned everything about Variables in JavaScript with its examples. In addition, we covered declaration, initialization and naming process. Along with this, we discussed reserved words and variable scopes. You may also like to know the 8 Unique Features of JavaScript.

Hope, you enjoyed our explanation. You can share your suggestions and ask queries in the comment section. Surely, we will get back to you.

Leave a Reply

Your email address will not be published. Required fields are marked *