Video Lesson

Lesson Notes

JavaScript Variables (var) Explained

The var keyword in JavaScript is an older way to create variables. It was commonly used in the past but is now considered a legacy practice.

Creating Variables with var

You create a variable by typing var, giving it a name, then assigning it a value:

1var age = 30; // storing a number 2var name = "Bob"; // storing text 3var isStudent = false; // storing true or false

Changing Values

With var, you can easily change the variable’s value later on:

1var score = 50; 2score = 75; // updating the value 3console.log(score); // Outputs: 75

Why is var considered legacy?

  • Scope Issues: Variables declared with var are function-scoped or globally-scoped, causing unexpected behavior.
  • Hoisting: var declarations get "hoisted," potentially causing confusing bugs.
  • Modern JavaScript uses let and const, providing clearer and safer ways to handle variables.

Recommendation:

  • Use let or const instead of var for clearer, safer, and more predictable code.

AI Assistant

Sign In Required

Sign in and subscribe to use the AI assistant for instant help with your lessons.

Sign In

Variables using the "var" keyword

A concise overview of JavaScript’s legacy var keyword—how to declare and update variables with it, why its function-scoped behavior and hoisting can lead to bugs, and why modern code prefers let and const for safer, more predictable variable handling.

2m 57s

Course Content

0 of 32 lessons completed
6. Promises, Async and Await
0 of 0 completed
7. The DOM
0 of 0 completed