DEV Community

Arpine Tadevosyan
Arpine Tadevosyan

Posted on

JavaScript Variables

To start with, a variable in JavaScript serves as a container for a strong data, like a number we might use in a multiplication, or a string that we might use as part of a sentence.

There are two types of variables in JavaScript : local variable(is visible only within a function where it is defined and accessible within the function) and global variable(can be defines anywhere in the code).

While declaring a JavaScript variable, there are some rules.

  1. Name of the variable must start with a letter, underscore( _ ), or dollar( $ ) sign.
  2. After first letter we can use digits (0 to 9), for example variable123.
  3. JavaScript variables are case sensitive, for example a and A are distinct variables.
  4. Reserved words (like JavaScript keywords) cannot be used as names.

Below you can see a simple example of JavaScript variable.
var m = 1;

var k = 4;

var z=m+k;

console.log(z);

VAR, LET, CONST

A little about the history.
The var keyword is used in all JavaScript code from 1995 to 2015.

The let and const keywords were added to JavaScript in 2015, therefore if you want your JavaScript code to run in older browsers, you must use var.

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

We use const when we know that the value should not be changed and is a general rule(moreover, when someone will look at our code and see const, they will immediately understand that this name will never be assigned to a different value. Any time they see this name, they will know what it refers to.).

"Use const when you can, and use let when you have to."

JavaScript Scope
Scope determines the accessibility of variables, there are 3 types of Scopes - Block scope, Function scope and Global scope.

Block Scope
let and const keywords provide Block Scope in JavaScript. (you can see that var is not included, the reason I will explain later)
Variables declared inside a { } block cannot be accessed from outside the block:
Example

{
let x = 6575;
}
// x can NOT be used here

var cannot have block scope, because var declares variable globally, for the whole code.
Example

{
var a = 111;
}
// a CAN be used here

Function Scope

  • var, let and const have function scope.
  • each function creates a new scope.
  • variables defined inside a function are not visible when we get out of function. Example

function ourFunction() {
let countryName = "Armenia"; // Function Scope
}

Global Scope
(a variable is global when it is defined outside a function)

Variables declared Globally have Global Scope.

In a JavaScript program, global variables can be accessed from anywhere.

Example

let bookName = "Xenty";
// code here can use bookName

function theFunction() {
// code here can also use bookName
}

Top comments (0)