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
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);
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.
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.
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:
- Learning JavaScript Course on OjamboShop.com
- Learning Python Course on OjamboShop.com
- Learning PHP Course on OjamboShop.com
- Learning JavaScript Ebook on Amazon
- Learning Python Ebook on Amazon
- Learning PHP Ebook on Amazon
- OjamboServices.com For Custom Websites, Applications & Tutorials