JavaScript Var Variable Declaration

Revised 2 min, 39 sec read

Declare Variables Using Var In JavaScript

The var keyword has been to declare variables since 1995.

JavaScript variables can be declared in 4 ways. Automatically, using var, let or const.

Var is used to declare variables in a function or global scope.

Variable Declaration

Glossary:

Scope

Refers to region of the code where variable is accessible.

Global Scope

Variables declared outside function or block are accessible anywhere.

Block Scope

Variables declared within block such as if or for statement are only accessible in that local scope.

Function Scope

Variables declared within function are only accessible in that local scope.

Initialization

Variables are initialized with a default value.

Redeclaration

Variables are redeclared in the same scope.

Declaring Variables

Var Variable Declaration
Name Description Example
var Declares function scope or global scope with optional initialization var num = 1;
Name Description Example

Var Variable Declaration Snippet

// OjamboShop.com Learning JavaScript Var Variable Declaration Tutorial
// Global Scope
var num1;
num1 = 1;
console.log(num1);
// Accessible Outside Block
if (true) {
	console.log(num1);
	var num2 = 2;
}
console.log(num2);
// Redeclare And Reassign Variable
var num1 = 11;
var num2 = 22;
console.log(num1);
console.log(num2);
// Hoisting 
{
	console.log(num3);
	var num3 = 3;
}
console.log(num3);

JavaScript Var Variable Declaration Code
OjamboShop.com Web IDE JavaScript Var Variable Declaration Code

JavaScript Var Variable Declaration Result
OjamboShop.com Web IDE JavaScript Var Variable Declaration Result


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE was used to input and compile JavaScript code for the var variable declaration.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Learn Programming Courses:

Courses are optimized for your web browser on any device.

OjamboShop.com Learning JavaScript Course
OjamboShop.com Learning JavaScript Interactive Online Course

Limited Time Offer:

OjamboShop.com is offering 20% off coupon code SCHOOL for Learning JavaScript Course until End Day 2024.

Learn Programming Ebooks:

Ebooks can be downloaded to your reader of choice.

OjamboShop.com Learning JavaScript Ebook
OjamboShop.com Learning JavaScript Ebook Cover Page

Conclusion:

JavaScript makes it easy to declare variables. Use var to declare function or global scoped variables with the optional initialization.

Take this opportunity to learn the JavaScript programming language by making a one-time purchase at Learning JavaScript Course. A web browser is the only thing needed to learn JavaScript in 2024 at your leisure. All the developer tools are provided right in your web browser.

If you prefer to download ebook versions for your reader then you may purchase at Learning JavaScript Ebook

References:

🚀 Recommended Resources


Disclosure: Some of the links above are referral links. I may earn a commission if you make a purchase at no extra cost to you.

About Edward

Edward is a software engineer, author, and designer dedicated to providing the actionable blueprints and real-world tools needed to navigate a shifting economic landscape.

With a provocative focus on the evolution of technology—boldly declaring that “programming is dead”—Edward’s latest work, The Recession Business Blueprint, serves as a strategic guide for modern entrepreneurship. His bibliography also includes Mastering Blender Python API and The Algorithmic Serpent.

Beyond the page, Edward produces open-source tool review videos and provides practical resources for the “build it yourself” movement.

📚 Explore His Books – Visit the Book Shop to grab your copies today.

💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.

🔨 Build it Yourself – Download Free Plans for Backyard Structures, Small Living, and Woodworking.