var name produces strange result in Javascript
It refers to window.name
, which is the name of the window.
You can use the window's name to target hyperlinks, but it's not typically useful.
More info on window.name
: https://developer.mozilla.org/en-US/docs/Web/API/Window.name
Just testing in chrome:
You can't stop var name
from being window.name
, which is a string. No matter what you set the value to, it will be cast as a string so that it is a valid window name. So, name.length
is the amount of characters in the string. It's best to avoid variable or be very careful about them!
As I can see from some of the other comments, this is an odd concept if you're new to it. The concern is over what window.name
refers to. window.name
is the name of the window. Any use of it is naming the window.
Defending that Chrome's behavior is logical:
If this var document = 'foo'
did what it looks like it would do, you would have overwritten window.document
- the document object - with a string. That would be quite the problem. name
is a property of window
just like document
and has a use that shouldn't be (and can't be in Chrome) replaced.
Javascript array variable when named name showing unexpected result when storing strings
Do not use name
as variable name, Because it will conflict with window.name
var fullName = "Jonathan Archer";var n = fullName.split(" ");console.log(n[0]);
//The output of the above code is : "J"
var userName = fullName.split(" ");console.log(userName[0]);
//The output of the above code is: "Jonathan"
//Also tried following, also exhibited same behavior as abovevar n = ["Jonathan", "Archer"];var userName = ["Jonathan", "Archer"];console.log(n[0]);console.log(userName[0]);
strange behaviour of variable named status in javascript
It's because you run your code in global context! var
bound variables are bound to the function scope. If you have no function you are in global context, which means in a browser you are on the window
object.
This code will log Demo
:
<script>
var foo = "Demo";
console.log(window.foo);
</script>
Now your code breaks because window.status
is reserved.
An easy fix is to surround your code by a function to provide a new context for your variables, which is always good practice.
<script>
(function() {
var status = [true,false,true,false,true,false,true,false,true,false];
var status1 = [true,false,true,false,true,false,true,false,true,false];
document.getElementById("demo1").innerHTML = status[2];
document.getElementById("demo2").innerHTML = status1[2];
})();
</script>
Why variable name is typed as string?
name
it's a property of global object window
and you cant replace it.
But you can delete
and then define again
delete window.name;
window.name = ()=>{console.log('ok')}
But i don't recommend change global properties.
In node environment all work good:
Javascript, var name = 1, typeof name gives string?
It is a behavior of the browser where some properties of window object like name and status will take only string values, if you assign any other type of values then the toString() value of that object is assigned to it
var name = 1;
console.log(typeof name); // this will alert "string"
var status = 1;
console.log(status, typeof status); //gives '1` and string
var status = {};
console.log(status, typeof status);//gives value of status as [object Object] since that is the toString() implementation of object
var b = 1;
console.log(typeof b); //
Demo: Fiddle
This behavior is not applicable if you use local variables... ie variables in a function
function test(){
var name = 1;
console.log(typeof name); // this will alert "string"
var status = 1;
console.log(status, typeof status); //gives '1` and string
var status = {};
console.log(status, typeof status);//gives value of status as [object Object] since that is the toString() implementation of object
var b = 1;
console.log(typeof b); //
}
test()
Demo: Fiddle
Using the variable name doesn't work with a JS object
window.name has a special purpose, and is supposed to be a string. Chrome seems to explicitly cast it to a string, so var name = {};
actually ends up giving the global variable name
(i.e. window.name
) a value of "[object Object]"
. Since it's a primitive, properties (name.FirstName
) won't "stick."
To get around this issue, don't use name
as a global variable.
Related Topics
JavaScript Infinitely Looping Slideshow with Delays
Create <Div> and Append <Div> Dynamically
"Var" or No "Var" in JavaScript's "For-In" Loop
JavaScript Use Variable as Object Name
How to Serialize an Object into a List of Url Query Parameters
Vuejs and Vue.Set(), Update Array
What Are the Current Cookie Limits in Modern Browsers
Es6: Conditional & Dynamic Import Statements
Is There a Sleep Function in JavaScript
Angularjs Multiple Filter with Custom Filter Function
Referenceerror: Fetch Is Not Defined
How to Give Keyboard Focus to a Div and Attach Keyboard Event Handlers to It
How to Break/Exit from a Each() Function in Jquery
JavaScript Regular Expressions and Sub-Matches
How to Pass Parameters to a Script Tag
Sanitizing User Input Before Adding It to the Dom in JavaScript