JS Variables


JavaScript Variables

What is a variable

  • Variables are like a container used to store information to be referenced and manipulated in a computer program.
  • Variables are one of the most often used elements of programs, so it is important to understand and use them well.
  • In JavaScript, we use the var, let, and const keywords to declare variables. Every variable has a name to distinguish it from other variables. It is possible to access a value by its variable name. You are allowed to assign a value to a variable during the time of its declaration. This can be done using the equals to symbol (=).

Variables Declaration

Before you start working with a variable, you need to declare (in other words, create) it. JavaScript uses Three keywords to create variables:

  • Var is used to declare a mutable variable the value of which can be changed as many times as needed. It is a function-scoped (When a variable is declared inside a function, it is only accessible within that function and cannot be used outside that function.) and
  • Let used to declare a mutable variable the value of which can be changed as many times as needed. It is a Block Scope (A variable when declared inside the if or switch conditions or inside for or while loops, are accessible within that particular condition or loop.)
  • Const is used to declare a constant whose value you want to forbid overwriting. It is a Block Scope (A variable when declared inside the if or switch conditions or inside for or while loops, are accessible within that particular condition or loop.)

When you declare a variable, you must give it a name after one of these keywords. It is good practice to assign a variable name that describes its contents. Always try to choose meaningful and readable names for variables to make your code easy to understand.

The following is the syntax for declaring and initializing variables in JavaScript:

var VariableName;

let VariableName;

const VariableName;

var VariableName = value;

let VariableName = value;

const VariableName = value;

 

We have used the var, let, and const keywords to declare the above three variables.

Three of the variables have been assigned a value and the other three are declared the variable. Note that this has been done at the time of their declaration.

x = 1;

y = "John" ;

 

It's not a good practice either. In the future, we will consider why it is a bad idea to declare variables without keywords, but for now, it is more useful to get acquainted with the constants.

Here are the rules that govern the declaration of variable names in JavaScript:

  • The variable name should begin with an underscore (_), a letter (either A to Z or a to z), or the dollar sign ($).
  • After the first letter, one can use digits in the variable name.
  • JavaScript variable names are case-sensitive, for example, p is different from P.

 The following example demonstrates how to use variables in JavaScript:

 <script>

var a = 10 ;

var b = 20 ;

var c= a + b;

document. write( c);

</script>

 

 The code will return the sum of the two variable values, which is 30.

Mutable variables

  • let allows you to declare variables that are limited to the scope of a block statement or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

let Name = "John"// define the variable and assign it a value

 

In variables, it is possible to store any type of data. In this case, we saved the string "John". You can refer to this line if you use a variable name:

let Name = "John";

 

console.log (Name); // outputs the content of the variable to the console

 

  • This code outputs the contents of the variable into the console. In our case, the result will be the string "John".
  • The case of a name is important: Name is not the same as a name.
  • Now let's try to change the variable and output a new value to the console:

let Name = "John";

Name = "Cena";

 

console.log (Name); // Cena

 

 

As you can see, the value of the variable has changed. Now it stores the string “Cina”.

  • var declarations, wherever they occur, are processed before any code is executed. This is called hosting and is discussed further below.

var Name = "John";

Name = "Cena";

console.log (Name); // Cena

 

  • The scope of a variable declared with var is its current execution context and closures thereof, which is either the enclosing function and functions declared within it or, for variables declared outside any function, global. Duplicate variable declarations using var will not trigger an error, even in strict mode, and the variable will not lose its value unless another assignment is performed.

Constants

Declare the immutable variable called language and add the string "JavaScript" as a value:

const Name = "John";

 

 

If we wanted to change the value of a variable created with the const keyword, we would have failed and got an error:

const Name = "John";

Name = "Cena"// error

 

Now that you know about variables, you will be able to create more complex and interesting programs.

  • Local Variables

These are variables that are defined within a block or a function. It can only be accessed from within the block or function within which it has been defined.

Here is an example:

 function XYZ ()

{

 var a= 12; // a local variable

 }

 

 

The variable a can only be accessed from within the

function XYZ()

 

Here is another example of local variable declaration:

if(5 < 10 )

{

var a = 12; //a JavaScript local variable

}

 

 

The variable a in the above example can only be accessed from within the block of its declaration.

  • Global Variables

These are variables that can be accessed from any function.

 A global variable is usually declared outside a function or with the JavaScript’s window object.

 For example:

var val= 12 ; //a global variable

function x()

{

document. writeln( val);

 }

function y()

{

document . writeln( val);

 }

 x(); //calling a JavaScript function

 y(); //calling a JavaScript function

 

 


Leave a comment
No Cmomments yet