JavaScript Variables

JavaScript Tutorial

What Are JavaScript Variables?

Variables in JavaScript are used to store data values such as text, numbers, or objects.

Think of a variable like a container that holds information.

Declaring JS Variables

Js uses three keywords to declare variables:

KeywordFeature
varOld way, function-scoped
letModern, block-scoped
constConstant (cannot be changed)

 Example:

Rules for Naming Variables

  • Must start with a letter, _ (underscore), or $

  • Cannot start with a number

  • Cannot use JavaScript reserved words (like class, return)

  • JavaScript is case-sensitive (nameName)

Example:

 Assigning Values

You can declare first, then assign later:

Or declare and assign in one line:

Changing Variable Values

let and var values can be updated:

But const cannot be changed:

 Js Variables Example with Output


 

Output:

Sum = 15

Summary

KeywordCan Change Value?Scope
varYesFunction scope
let YesBlock scope
const NoBlock scope

You may also like...