0
0
Javascriptprogramming~5 mins

Why variables are needed in Javascript

Choose your learning style9 modes available
Introduction

Variables help us store and remember information in a program. They let us use that information later or change it as needed.

When you want to remember a user's name to greet them later.
When you need to keep track of a score in a game.
When you want to store a number that changes, like a counter.
When you want to reuse a value multiple times without typing it again.
When you want to make your code easier to read and understand.
Syntax
Javascript
let variableName = value;

let is used to create a variable you can change later.

variableName is the name you give to the variable.

Examples
This creates a variable named age and stores the number 25 in it.
Javascript
let age = 25;
This stores the text "Alice" in a variable called name.
Javascript
let name = "Alice";
This starts a score at 0, then adds 10 to it.
Javascript
let score = 0;
score = score + 10;
Sample Program

This program stores a name and a number in variables. It then prints messages using those variables. It also shows how to change a variable's value.

Javascript
let userName = "Sam";
console.log("Hello, " + userName + "!");

let counter = 1;
console.log("Counter is at: " + counter);
counter = counter + 1;
console.log("Counter is now: " + counter);
OutputSuccess
Important Notes

Variable names should be clear and describe what they hold.

You can change the value stored in a variable anytime.

Using variables makes your code easier to fix and update.

Summary

Variables store information to use later in your program.

They make your code flexible and easier to understand.

You can change what a variable holds as your program runs.