What is a JavaScript Variable and How to declare it?

In this article, we discuss What is a JavaScript Variable and How to declare it?

What is a JavaScript Variable and How to declare it?

What is a JavaScript Variable and How to declare it?
What is a JavaScript Variable and How to declare it?

What is a JavaScript Variable and How to declare it?

Just like in algebra, we use variables in programming languages to hold values. JavaScript includes variables that are used to hold the data value and it can also be changed anytime. In this article, we will discuss about different JavaScript Variables and how these reserved keywords are used as variables in the following sequence:

  • What is a JavaScript Variable?
  • How to declare a variable?
  • Identifiers
  • JavaScript Variable Scope
  • Reserved Words

What is a JavaScript Variable and How to declare it?

What is a JavaScript Variable?

Variables can be called as named containers. You can place data into these containers and then refer to the data simply by naming the container. It holds the reusable data and it is also called as the basic unit of storage in a program.

  • The value stored in a variable can be changed during program execution.
  • A variable is just a name given to a memory location. And all the operations done on the variable affects the memory location.
  • The variables must be declared before they can be used in JavaScript.

You can create a variable in JavaScript as:

var var_name;
var x;

Here, x is a variable name that can be used to store any value. You can also declare multiple variables as:

var name, title, empid;

How to declare a JavaScript variable?

JavaScript also allows variable declaration without var keyword. But, you must assign a value when you declare a variable without var keyword.

empid = 701;

Although, it is not recommended to declare a variable without var keyword. It may accidentally overwrite an existing global variable

What is a JavaScript Variable and How to declare it?

JavaScript Identifiers

You need to identify JavaScript variables with unique names. These unique names are called identifiers. You can provide short names or descriptive names for your identifier. Some rules you must keep in mind while naming JavaScript Identifiers are:

  • Names can contain letters, digits, underscores, and dollar signs
  • Names must begin with a letter
  • The identifier names can also begin with $ and _
  • Names are case sensitive. Thus, y and Y are different variables
  • Reserved words cannot be used as names

JavaScript Variable Scope

Scope of a variable is the part of the program from where the variable can be accessed directly. There are two types of scopes in JavaScript:

Global Scope – A global variable has a global scope which means it can be defined anywhere in your JavaScript code.

Example:

<script>  
var data=100;//gloabal variable  
function one(){  
document.writeln(data);  
}  
function two(){  
document.writeln(data);  
}  
one();//calling JavaScript function  
two();  
</script> 

Local Scope – A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

Example:

<script>
function localvar(){
var x=20;//local variable
}
</script>

//example:
  
let globalVar = "This is a global variable";
function fun() {
let localVar = "This is a local variable";
}
fun();
console.log(globalVar);
console.log(localVar);

It will give the output as:

stack
What is a JavaScript Variable and How to declare it?

The console.log statements are present in the global scope where they have access to global variables but cannot access the local variables.

Reserved Words

The reserved words cannot be used as JavaScript variables, functions, methods, loop labels, or any object names. Here is a list of such reserved words in JavaScript:

abstractbytefinallyfunctionimplements
elseextendsnullprotectedshort
instanceoflongtruevarwhile
switchthrowclassdebuggerdo
booleanfalsefloatgotoimport
enumnativepackagepublicstatic
intthrowstryvoidwith
synchronizedcatchconstdefaultdouble
breakfinalforifin
exportnewprivatereturnsuper
interfacetransienttypeofvolatile
thischarcontinuedelete
What is a JavaScript Variable and How to declare it?

With this, we have come to the end of our article. I hope you understood What is a JavaScript Variable and How to declare it?

Source: Various

Also Check out What Is JavaScript? A Guide for Total Beginners 2021

If you interested in Entertainment genre please check this also Alia Bhatt Age, Biography, Wiki, Photos, Movies & More

Related Posts

3 thoughts on “What is a JavaScript Variable and How to declare it?

Leave a Reply

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