When you use var it is "hoisted" to the top of the function declaration. Let's look at the second example again:
var myvar = "my value";
(function() {
console.log(myvar); // undefined
var myvar = "local value";
})();
Notice how var myvar = 'my value' is declared first. Next, within the function scope, console.log(myvar) is called. The result is "undefined." Why? You'd think it would be "my value" because that's the order that the code is in.
Because the local variable var myvar in the function scope is hoisted, it's not defined. This is essentially equivalent to writing the function like this:
(function() {
var myvar;
console.log(myvar); // undefined
myvar = "local value";
})();
Answer from Explosion Pills on Stack OverflowMozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Grammar_and_types
Grammar and types - JavaScript - MDN - Mozilla
For example, const { bar } = foo. This will create a variable named bar and assign to it the value corresponding to the key of the same name from our object foo. Variables should always be declared before they are used. JavaScript used to allow assigning to undeclared variables, which creates ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Variables
Storing the information you need — Variables - Learn web development | MDN
August 18, 2025 - JavaScript is a "dynamically typed language", which means that, unlike some other languages, you don't need to specify what data type a variable will contain (numbers, strings, arrays, etc.). For example, if you declare a variable and give it a value enclosed in quotes, the browser treats the variable as a string:
Videos
JavaScript Variables - Part 1 - What are Variables ...
00:33
JavaScript Variables - Part 1 - What are Variables? - #w3schools ...
JavaScript - Variables - W3Schools.com
JavaScript Syntax - Part 1 - Variables - #w3schools #javascript ...
00:55
JavaScript Variables - Part 5 - Adding Numbers & Strings - #w3schools ...
JavaScript Variables - Part 2 - var, let & const - #w3schools ...
Stack Overflow
stackoverflow.com › questions › 48160226 › mdn-javascript-exercise-using-additional-variable
MDN JavaScript exercise using additional variable - Stack Overflow
just trying to learn some JS via the MDN website and a bit confused to why they are using one variable to store the result of the loop and then again another variable to store 2nd variable, see example · https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › var
var - JavaScript | MDN - Mozilla
This allows creating multiple variables at once. ... const result = /(a+)(b+)(c+)/.exec("aaabcc"); var [, a, b, c] = result; console.log(a, b, c); // "aaa" "b" "cc" For more information, see Destructuring. ... This page was last modified on Jul 8, 2025 by MDN contributors.
MDN Web Docs
developer.mozilla.org › en-US › docs › MDN › Writing_guidelines › Code_style_guide › JavaScript
Guidelines for writing JavaScript code examples - MDN Web Docs | MDN
When iterating through all collection elements, avoid using the classical for (;;) loop; prefer for...of or forEach(). Note that if you are using a collection that is not an Array, you have to check that for...of is actually supported (it requires the variable to be iterable), or that the forEach() method is actually present. ... Do not use for (;;) — not only do you have to add an extra index, i, but you also have to track the length of the array. This can be error-prone for beginners. ... Make sure that you define the initializer properly by using the const keyword for for...of or let for the other loops. Don't omit it. These are correct examples:
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Variable
Variable - Glossary | MDN - Mozilla
Declaring variables in JavaScript · var statement in JavaScript · Was this page helpful to you? Yes · No Learn how to contribute · This page was last modified on Jul 11, 2025 by MDN contributors.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › var
<var>: The Variable element - HTML | MDN - Mozilla
July 9, 2025 - ... <p> The variables <var>minSpeed</var> and <var>maxSpeed</var> control the minimum and maximum speed of the apparatus in revolutions per minute (RPM). </p> This HTML uses <var> to enclose the names of two variables. ... This page was last modified on Jul 9, 2025 by MDN contributors.
Unibo
lia.disi.unibo.it › materiale › JS › developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › var.html
var - JavaScript | MDN
April 12, 2015 - The variable statement declares a variable, optionally initializing it to a value.
GitHub
github.com › mdn › content › blob › main › files › en-us › web › javascript › reference › statements › var › index.md
content/files/en-us/web/javascript/reference/statements/var/index.md at main · mdn/content
Be careful of the `var x = y = 1` syntax — `y` is not actually declared as a variable, so `y = 1` is an [unqualified identifier assignment](/en-US/docs/Web/JavaScript/Reference/Operators/Assignment#unqualified_identifier_assignment), which ...
Author mdn
W3Schools
w3schools.com › js › js_variables.asp
JavaScript Variables
Variables are identified with unique names called identifiers. Names can be short like x, y, z. Names can be descriptive age, sum, carName. The rules for constructing names (identifiers) are: Names can contain letters, digits, underscores, and dollar signs. Names must begin with a letter, a $ sign or an underscore (_). Names are case sensitive (X is different from x). Reserved words (JavaScript keywords) cannot be used as names.
Scribd
scribd.com › document › 654504717 › VAR-JavaScript-MDN
VAR - JavaScript - MDN | PDF | Scope (Computer Science)
March 14, 2023 - JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements
Statements and declarations - JavaScript - MDN Web Docs
February 2, 2026 - The fact that var is a statement instead of a declaration is a special case, because it doesn't follow normal lexical scoping rules and may create side effects — in the form of creating global variables, mutating existing var-defined variables, and defining variables that are visible outside of its block (because var-defined variables aren't block-scoped). As another example, labels can only be attached to statements. ... Note: There's a legacy grammar that allows function declarations to have labels, but it's only standardized for compatibility with web reality. To get around this, you can wrap the declaration in braces — this makes it part of a block statement. ... This page was last modified on Jul 29, 2025 by MDN contributors.
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Local_variable
Local variable - Glossary - MDN Web Docs
July 11, 2025 - A variable whose name is bound to its value only within a local scope. ... This page was last modified on Jul 11, 2025 by MDN contributors.
Turing
frontend.turing.edu › lessons › module-1 › js-data-types-and-variables.html
JS: Data Types & Variables - Front-End Engineering Curriculum - Turing School of Software and Design
Review what you know about the primitive data types used in JavaScript · Review how to create and use variables to store values
W3Schools
w3schools.com › js › js_datatypes.asp
JavaScript Data Types
... JavaScript evaluates expressions from left to right. Different sequences can produce different results: ... In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo".
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Variables
3 weeks ago - But in the old times, it was technically possible to create a variable by a mere assignment of the value without using let. This still works now if we don’t put use strict in our scripts to maintain compatibility with old scripts. // note: no "use strict" in this example num = 5; // the variable "num" is created if it didn't exist alert(num); // 5
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Data_structures
JavaScript data types and data structures - JavaScript | MDN
All primitive types, except null and undefined, have their corresponding object wrapper types, which provide useful methods for working with the primitive values. For example, the Number object provides methods like toExponential(). When a property is accessed on a primitive value, JavaScript automatically wraps the value into the corresponding wrapper object and accesses the property on the object instead.