JavaScript Variables

๐Ÿง  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 JavaScript Variables

JavaScript uses three keywords to declare variables:

Keyword Feature
var Old way, function-scoped
let Modern, block-scoped
const Constant (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 (name โ‰  Name)

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:


๐Ÿ–ฅ JavaScript Variables Example with Output


 

Output:

Sum = 15

๐Ÿงญ Summary

Keyword Can Change Value? Scope
var โœ” Yes Function scope
let โœ” Yes Block scope
const โŒ No Block scope

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *