Why does Javascript cast null to string at variable assignment? [duplicate] - javascript

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
Closed 6 years ago.
In Firefox 45 on OSX, when I fetch an item from localStorage from a key that does not exist, the function call returns null. I tested this in the console.
If I instead assign the call result to a variable, and print its value in the console, I get "null", i.e. a string.
Why does a variable assignment of a previously not defined variable cast a call result to a String?
Used code (in the console):
localStorage.getItem("non-existing-key"); // returns null
var x = localStorage.getItem("non-existing-key");
x // returns "null"
Edit: both versions seem to behave correctly on Chrome 50.0.2661.86 on OSX (both return null)
Edit2: my mistake. I used another variable name in my tests (specifically: var name). Now, if I let the console return the value of the variable name, it returns window.name, which is a property of window of the type String, defaulting to "null". So, it's not an assignment that causes a cast, but instead its that I got a String property defined by window.

I made a mistake. The specific code I used was the following:
var name = localStorage.getItem("non-existing-key");
name
Now, getItem does return null and not a String. What then happens is that by letting the console print the value of name it does in fact get window.name (see window.name on MDN), which by default is "null" (a String).

Related

A bug within the console with typeof? [duplicate]

This question already has answers here:
Why is typeof null "object"?
(11 answers)
Closed last month.
I'm still sort of new to JavaScript and so I was playing around with some Javascript. I used let car = null; and had console.log(car) into the console which came up to null of course. I then console.log(typeof car) into the console this time but came back as an object. I was wondering why since I let car = null?
let car = null; console.log(car); //came back as null console.log(typeof car); //came back as object
So I thought that would come back as null as well after I console.log(typeof car); but didn't and came back as an object.
It's the behavior of this operator - check MDN Page
w3schools states the following
In JavaScript null is "nothing". It is supposed to be something that
doesn't exist.
Unfortunately, in JavaScript, the data type of null is an object.
You can consider it a bug in JavaScript that typeof null is an object.
It should be null.

Check for undefined variable with part of it's name being a variable, in jQuery [duplicate]

This question already has answers here:
Dynamic variable name in loop
(3 answers)
Javascript use variable as object name
(15 answers)
Closed 4 years ago.
I have the following jQuery snippet which sets the 'selectedIndex' property of six select elements to '0'.
for (m = 1; m <= 6; m++) {
$("#po"+eval("leafWId"+m)).prop("selectedIndex", 0);
}
In the snippet scope there are among others the following variables defined:
leafWId1, leafWId2, leafWId3, leafWId4 with each one of these set to a different number, so the part
eval("leafWId"+m)
in each iteration, is equivalent to
eval("leafWId1"), eval("leafWId2"), eval("leafWId3")........
and thus, evaluates to each of the numbers referred above, and the snippet
$("#po"+eval("leafWId"+m))
returns a jQuery object consisting of select elements with id value of the type: "po345" for example.
Now, when the 'm' variable is set through the iteration to value '5', I get an Uncaught Reference Error, reporting that "leafWId5 is not defined" (as expected, since only four variables are defined, ie, leafWId1, leafWId2, leafWId3, leafWId4 as mentioned above).
I want to add a conditional statement that will check for an undefined variable whose name will be the result of the evaluation
eval("leafWId"+m)
and if is defined, then use the prop() method, if not, skip this, so I dont' get the Reference Error.
Or as an alternative, maybe check for the length of the jQuery object below
$("#po"+eval("leafWId"+m))
and proceed with the prop() method only when it's length is greater than zero.
Actually, the problem I face is that when m variable is set to '5', the part
eval("leafWId"+m)
evaluates to 'leafWId5', a variable that is not defined, and the snippet does not let me check for 'undefined' value in advance, because the eval() function gives an error, so any conditional statement I have used that checks for existing variable and uses the eval() function does not work.

JavaScript property access that throws an error [duplicate]

This question already has answers here:
Why does reading a property sometimes throw an error in javascript?
(5 answers)
Closed 1 year ago.
In which specific cases does property access throw an error in JavaScript?
In Node.js, this prints undefined:
x = 3
console.log( x.thing );
This throws an error:
x = null;
console.log( x.thing );
What exactly is the semantics here? Property access is normal behavior for almost all values—even functions—but on undefined and null it throws an error.
I can't for the life of me find confirmation that those are the only cases. Can anybody confirm that?
undefined and null are not references to objects, nor are they primitive values that can be implicitly boxed in object wrappers. Thus, any attempt to reference a property is going to fail.
When you use a number (3), the runtime boxes that as a Number instance, but of course there's no "thing" property so the value is undefined.
Also, functions are first-class objects, so references to properties on functions are not really "weird" in any sense.

JS Array called "name" behaves strangely [duplicate]

This question already has answers here:
Is variable called "name" always defined in Javascript?
(2 answers)
Closed 6 years ago.
I played around a bit and created a javascript array containing some strings. When I tried to access the array it behaves quite strangely. The array is created correctly and works as expected if it is called something else.
var name = ["foo", "bar"];
alert(name); // shows "foo,bar"
Why is the array converted into a string, if the variable name is name? According to the standard (which the linked website is based on) it should be a valid variable name: https://mothereff.in/js-variables#name
If you execute javascript in a browser environment, the code is executed in the window context. This context already has some global variables set. One of those is the window.name which is a string. When the variable is set, the browser will automatically cast the new value to string which causes the strange behavior.
So even though name is a valid variable name, it should not be used in the global context if you are executing your javascript in the browser (It should work fine in e.g. node.js).

Date object with variable says undefined [duplicate]

This question already has an answer here:
Console returns undefined [duplicate]
(1 answer)
Closed 8 years ago.
Why I am getting undefined for this below statement in console?
var someDate=new Date(1337986800000);
But with out assigning to a variable it works fine
new Date(1337986800000);
Why is it so?
Just type this:
var someDate=new Date(1337986800000); someDate;
It is how the console works.
When you are doing just new Date(1337986800000);, the constructor is returning the object which is printed in the screen.
but when you assign it to a variable, the variable holds the return value, so the console has nothing to do but print undefined. So you'll need to explicitly call the variable to get the output which you are expecting
What you do is create a new Date instance. In the first example you store the instance in a variable; you get undefined because the constructor function itself doesn't explicitly return anything.
In the second example you ask the console to evaluate an expression, which is calling the date constructor, so it just returns you the resulting instance.

Categories

Resources