Featured Article

Master W3Schools JS Var Quick Guide

Adam Jul 13, 2026

When learning JavaScript (JS), one of the first concepts you encounter is the 'var' keyword, used for variable declaration. W3Schools, a popular online learning resource, offers an excellent guide on this topic, making it an ideal starting point for beginners.

js.
js.

This article aims to delve into 'var' in JavaScript using W3Schools as our primary reference, ensuring you understand its functionality and best practices. Let's dive right in.

studious 📒📑
studious 📒📑

Understanding the 'var' Keyword

The 'var' keyword is used to declare a variable in JavaScript. It's crucial to comprehend its scope and hoisting.

Pins
Pins

Variables declared with 'var' are function-scoped, meaning they're only accessible within the function they're declared in. Before ES6, this was the only way to create variables with a limited scope.

Variable Hoisting with 'var'

two people sitting on the grass in front of a large brick building with tall towers
two people sitting on the grass in front of a large brick building with tall towers

'var' is subject to hoisting, a JavaScript feature where variable and function declarations are moved to the top of their containing scope during the compilation phase. This means you can use a variable before it's declared in your code.

For instance:

<script>
console.log(x);  // Outputs: undefined
var x = 5;
</script>

Initializing Variables with 'var'

the us school system - by age chart is shown in this screenshote image
the us school system - by age chart is shown in this screenshote image

It's a good practice to initialize variables with 'var' when declaring them. This avoids unexpected behaviors due to hoisting. Here's how you do it:

<script>
var x;  // Declaration
x = 5;  // Initialization
</script>

Width and Scope of 'var'

Before ES6, 'var' was the only way to create variables with a limited scope. This changed with the introduction of 'let' and 'const'.

a computer screen with the words public class main
a computer screen with the words public class main

It's essential to understand the width and scope of 'var' to avoid common pitfalls. Misuse can lead to unexpected behavior and bugs in your code.

Global Scope with 'var'

School British, Uk Highschool Aesthetic, Uk Public School Aesthetic, British Secondary School Core, Uk Private School, School British Aesthetic, Uk High School, British Secondary School Aesthetic, British High School Aesthetic
School British, Uk Highschool Aesthetic, Uk Public School Aesthetic, British Secondary School Core, Uk Private School, School British Aesthetic, Uk High School, British Secondary School Aesthetic, British High School Aesthetic
gerlinger hall i love you dearly
gerlinger hall i love you dearly
a computer screen with a program running on it
a computer screen with a program running on it
Theme with Variables
Theme with Variables
Sadie Sink High School, High School Friends Aesthetic, Fun High School Life Aesthetic, High School Aesthetic Friends, High School Best Friends Aesthetic, Highschool Aesthetic Friends, Friends Talking At School, American Middle School Aesthetic, School Besties Aesthetic
Sadie Sink High School, High School Friends Aesthetic, Fun High School Life Aesthetic, High School Aesthetic Friends, High School Best Friends Aesthetic, Highschool Aesthetic Friends, Friends Talking At School, American Middle School Aesthetic, School Besties Aesthetic
@russianlyfe
@russianlyfe
a group of young people standing in front of a building with the words jay group of schools on it
a group of young people standing in front of a building with the words jay group of schools on it
a table that has some type of text on it, including the words curl
a table that has some type of text on it, including the words curl
wake up!!!
wake up!!!

If you declare a variable with 'var' outside any function, it becomes a global variable. This can lead to namespace pollution and unexpected behavior.

Here's an example:

<script>
var y = 10;
function test() {
  console.log(y);  // Outputs: 10
}
test();
</script>

Function-Level Scope with 'var'

Variables declared with 'var' inside a function are only accessible within that function. This is known as function-level scope.

Best Practices with 'var'

Given the introduction of 'let' and 'const' in ES6, using 'var' is generally discouraged in modern JavaScript. However, understanding 'var' is still crucial for legacy code and compatibility issues.

Here are some best practices:

  • Always initialize your variables.
  • Use 'let' and 'const' instead of 'var' in new code.
  • Be mindful of global variables. Use 'use strict'; to prevent unintended global variables.

In conclusion, while 'var' in JavaScript might not be the preferred choice in modern coding, understanding its functionality and scope is vital for understanding JavaScript's evolution and for working with legacy code. Happy coding!