JavaScript and JQuery Essentials Trainin... (56 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (29 Blogs)
  • jQuery UI Development (2 Blogs)
  • The Complete WebDeveloper (38 Blogs)
  • ReactJS (7 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is a JavaScript Variable and How to declare it?

Published on Aug 26,2019 70 Views
3 / 29 Blog from JavaScript

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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?

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.

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>
 
Let&rsquo;s have a look at the following 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:

Scope output - javascript variable - edureka

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:

abstract

byte

finally

function

implements

else

extends

null

protected

short

instanceof

long

true

var

while

switch

throw

class

debugger

do

boolean

false

float

goto

import

enum

native

package

public

static

int

throws

try

void

with

synchronized

catch

const

default

double

break

final

for

if

in

export

new

private

return

super

interface

transient

typeof

volatile

this

char

continue

delete

 

With this, we have come to the end of our article. I hope you understood what is JavaScript Variable and how it is used.

Now that you know about JavaScript Loops, check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3). 

Got a question for us? Please mention it in the comments section of “JavaScript Variable” and we will get back to you.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.