DEV Community

Cover image for Variables in JavaScript
Rakshambika
Rakshambika

Posted on

Variables in JavaScript

What is Variables?

  • Variables are the container that are used to store the value.
  • The stored values can be accessed, updated, and reused whenever needed.

Rules for Naming Variables in JavaScript

  1. Variable names can contain letters, digits, _ (underscore), and $ (dollar sign).
  2. Variable names must start with a letter, _, or $.
  3. Variable names cannot contain spaces.
  4. Variable names cannot use special characters except _ and $.
  5. Variable names are case-sensitive.
  6. Reserved keywords cannot be used as variable names.

There are three ways to declare variables:

  • Let
  • Const
  • Var

Important things about variables:

1. Variable Declaration:

Creating a variable using let, const, or var.

let and var can be declared first and initialized later.

const must be initialized at the time of declaration.

let name;
const pi = 3.14;
var age;
Enter fullscreen mode Exit fullscreen mode

2. Variable Initialization

Assigning a value to a variable.

name="Cat";
age=22;
Enter fullscreen mode Exit fullscreen mode

3. Variable Reassignment

Changing the value of a variable.

var age = 22;
age = 23;

let name="Cat";
name="Parrot";

Enter fullscreen mode Exit fullscreen mode

Excited to continue learning and building with JavaScript!

Top comments (0)