02 - JavaScript - Variables

02 - JavaScript - Variables

JavaScript variables are used to store data values that can be used throughout a program. A variable is declared by using the "var", "let", or "const" keyword, followed by the variable name. The name of a variable can be any combination of letters, numbers, and underscores, but it cannot start with a number. JavaScript variables are case-sensitive, so "myVariable" and "myvariable" are two different variables.

The "var" keyword was the original way to declare variables in JavaScript. However, "let" and "const" were introduced in ES6 as more modern alternatives. "let" is used to declare variables that can be reassigned, while "const" is used to declare variables that cannot be reassigned.

Variables can store different types of data, including strings, numbers, arrays, objects, and functions. JavaScript uses dynamic typing, which means that the data type of a variable is determined at runtime, based on the value that is assigned to it.

It's important to note that variables have a scope, which determines where they can be accessed. Variables declared with "var" have function scope, while variables declared with "let" and "const" have block scope. This means that variables declared inside a block of code (e.g., within curly braces) are only accessible within that block.

In conclusion, JavaScript variables are a fundamental concept in programming. They allow us to store and manipulate data values, which is essential for building dynamic and interactive web applications. By understanding the different types of variables and their scope, developers can write more efficient and maintainable code.

Variables and Its Declaration

Variables are like containers for storing data values. Let's see the below examples for declaring variables using var, let and const.

The "const" keyword is generally used for declaring Constant values i.e when any variable is declared with a const keyword, its value cannot be changed after its declaration.

const a = 100;
const b = 200;
const c = 300;

The main difference between the keywords "var" and "const" is that variables declared by "let" are only available inside the block where they're defined. Variables declared by "var" are available throughout the function in which they're declared.

function varScopeCheking() {
  var x = 10;

  if (true) {
    var x = 20;
    console.log(x); // will print 20
  }

  console.log(x); // will print 20
}

function letScopeChecking() {
  let x = 10;

  if (true) {
    let x = 20;
    console.log(x); // will print 20
  }

  console.log(x); // will print 10
}

function varAndLetScopeChecking() {
  var x = 10;

  if (true) {
    let x = 20;
    console.log(x); // will print 20
  }

  console.log(x); // will print 10
}

JavaScript Identifiers

All variables in JavaScript are identified with unique names and these unique names are called Identifiers.

Some of the general rules for naming variables are as follows -

  • Name could contain letters, numbers, _ underscore and $ dollar sign.

  • Name must begin with letter.

  • Name could begin with $ and _ sign.

  • Names are case-sensitive.

  • Some of the reserved keywords in the JS couldn't be used as names.

JavaScript "let" Keyword

Some of the general rules for the "let" keyword are as follows -

  • Variables declared with the "let" keyword cannot be re-declared.

  • Variables declared with the "let" keyword must be declared before use.

  • Variables declared with the "let" keyword have a block scope.

let x = 10;
// Here x is 10

{
let x = 2;
// Here x is 2
}

// Here x is 10

JavaScript "const" Keyword

Some of the general rules for the "const" keyword are as follows -

  • Variables declared with the "const" keyword cannot be re-declared.

  • Variables declared with the "const" keyword cannot be re-assigned.

  • Variables declared with the "const" keyword have a block scope.

const x = 10;
// Here x is 10

{
const x = 2;
// Here x is 2
}

// Here x is 10

JavaScript "var" Keyword

Some of the limitations of the "var" keyword are as follows -

  • Redeclaring a variable using the "var" keyword can impose problems.

  • Redeclaring a variable using the "var" keyword inside a block will also redeclare the variable outside the block.

var x = 10;
// Here x is 10

{
var x = 2;
// Here x is 2
}

// Here x is 2

Finishing Up

Thank you for reading my article! I hope this has given you some insight into some of the basics of JavaScript Variables. In the next part, we would explore some of the JavaScript Operators.

Did you find this article valuable?

Support manas krishna jaiswal by becoming a sponsor. Any amount is appreciated!