Variables in Javascript

Javascript

Imagine variables as labeled boxes, each capable of storing and manipulating specific types of information. In programming, variables serve as the backbone, facilitating operations, decision-making processes, and the creation of dynamic applications. They are the building blocks that allow you to make your code come to life.

Variables in JS

JavaScript, which is predominantly used in web development, heavily relies on the use of variables. Whether you’re manipulating the content on a webpage or handling user interactions, understanding how variables function in JavaScript is key to writing efficient and effective code.

There are 4 ways of declaring Variables:

  1. Automatically
  2. Using var
  3. Using let
  4. Using const

When declaring JavaScript variables, also known as identifiers, it’s important to follow certain rules to ensure proper functionality and maintainability of your code. Here are the key rules to keep in mind:

  1. Start with a Letter, Underscore, or Dollar Sign:
    • Variable names must begin with a letter, an underscore (_), or a dollar sign ($).
    • Examples: user, _value, $count.
  2. Subsequent Characters:
    • After the initial character, variable names can include letters, numbers, underscores, or dollar signs.
    • Examples: user123, total_count, _value1, $amount.
  3. Avoid Reserved Keywords:
    • Do not use reserved keywords as variable names. Reserved keywords are part of the JavaScript language and have special meanings.
    • Examples of reserved keywords: function, let, if, else, for, while, etc.
  4. Case-Sensitive:
    • JavaScript is case-sensitive, so myVariable and myvariable are considered different variables.
    • Be consistent in your naming conventions.

Here are some example:

let user = "John"; // Starting with a Letter
let _value1 = 20; // Starting with underscore and Subsequent character 
let $amount = 100; // Starting with dollar sign

Two Types of Variables in JavaScript

1. Local Variable

Local variables are confined to specific blocks of code, typically within functions. They exist only within the scope of that function, preventing unintentional interference with other parts of your program. This encapsulation is crucial for maintaining a clean and organized code structure.

Example:

function exampleFunction() {
    var localVar = "I am a local variable";
    // localVar is only accessible within this function
}

2. Global Variable

Global variables, in contrast, have a broader scope and can be accessed from any part of your code. While convenient, their extensive reach comes with a responsibility to use them judiciously to avoid unintended side effects and potential conflicts.

Example:

var globalVar = "I am a global variable";
// globalVar can be accessed from any part of the code

Some more information about var, let and const.

MethodDescription
varUsed for variable declaration in older JavaScript versions. Consider using let or const in modern coding.
letIntroduced in ES6, ideal for variables that can be reassigned.
constAlso introduced in ES6, best for variables that should remain constant throughout the program.