X



Home>Tutorial>

What is JavaScript variables and Types of JavaScript variables

Nuelson Penuel Saturday, August 14, 2021 Javascript

Introduction

In this brief tutorial, you will learn about what is JavaScript variables, types of JavaScript variables, variable naming rules and differences between local and global variables.

What is a JavaScript Variable

A JavaScript variable simply means a placeholder of values that are subject to change at runtime. When we talk of variable, we are referring to a placeholder of values that could either be entered by a user or generated by the JavaScript engine based on the developer specifications.

Javascript Variable Naming Rules or Naming Convention

Like every other programming or scripting languages, variable declaration or naming has some rules that must be strictly adhere to and it is called variable naming rules or convention. You must have seen someone asking what are the rules for naming a variable in JavaScript or you probably asked this question why delving into programming. These rules are:
  1. A variable name must not contain any of the special characters except underscore(_) and dollar sign ($)
  2. A variable name must not start with numeric value
  3. A variable must not be ambiguous.
  4. In JavaScript, a variable name is case sensitive. Var Ab is not same as Var ab.
  5. A variable name must not contain space e.t.c.

Example of JavaScript variables

var a;
const b;
var a,b;
var sum;

What are the types of JavaScript variables

Generally, there are two types of variables in JavaScript namely local variable and global variable.

What is local variable in JavaScript A local variable as the name implies can only be access within the function it is declared. Local variables are available for use throughout the function it appears. In other words, it can't be called from another function.

Example of JavaScript local variables

function sum(){
var a=2;
var b=3;
var r= parseInt(a) + parseInt(b);
//result is 5
}
//In the function sum, we declared<b> a,b,r as local variables and as such, can be accessed <br>through out the function. That is why we have r = a + b.
function sub(){
r= parseInt(a) - parseInt(b);
//result is null
}
//The reason our result is null is because we tried accessing variable a,b and r from another function where it is not declared.
I believed you have gotten the idea from the above explanation. Now, let's move over to global variables.

What is a Global Variable in JavaScript

A global variable is a variable that can be accessed through out the JavaScript file. All functions that are contained in the JavaScript file can access the variable values anywhere. For more clarification, consider a global variable as a big with many doors yet you have a spare key for all doors. Bro, you can open any of these doors whenever you like. That is the true nature of global variables. They let you access them without restrictions. Lol!

Example of JavaScript global variables

var a=2;
var b=3;
var r;
function sum(){
r = parseInt(a) + parseInt(b);
//result is 5
}
//In the function sum, we declared<b> a,b,r as global variables and as such, can be accessed by all functions. That is why we have r = a + b.
function sub(){
r= parseInt(a) - parseInt(b);
//result is -1
}
//The reason our result is not null this time is because we declared a,b and r as global variables.
I believed you have gotten the idea from the above explanation.

What is the Difference between Local Variables and Global Variables

  1. Global variables are declared outdoide a function. While local variables are declared inside a fun function.
  2. Global variables can be accessed anywhere. While local variable can only be used within the function it is declared.

| | Views(281)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services