JS: difference between undefined and `undefined` - javascript

Looking at this code; why doesn't a satisfy (a === typeof a)
var a;
(a === undefined)?console.log("a is undefined"):null;
(typeof a === 'undefined')?console.log("typeof a is 'undefined'"):null;

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof typeof always returns a string.

Because:
var a;
typeof a === 'undefined';
a === undefined;
One is a string with the string value 'undefined', one is the undefined primitive. Those two are not the same.
typeof x always returns string values such as "undefined", "boolean", "string", "object", etc....

Related

Difference between typeof === and typeof ==

What is the difference in using typeof === or typeof == or is same thing?, since both return the same value.
function main() {
var number = 10;
console.log(typeof number == 'number'); // true
console.log(typeof number === 'number'); // true
}
main();
The typeof operator always returns a string. The === compares both the value and the type whereas == operator only compares the value.
This means both the == and === comparators will always act the same when you have typeof and a string on two sides of them.
Prefer using the strict equality comparison === as is a subset of the ==. The latter does type conversations like null and undefined being equal which can be surprising.
=== makes a strict comparison and only returns true if the compared elements have the same value and type
== compares only in terms of values, not type of data

typeof typeof x returns string and not object since type of null is object

I am new to js, trying to learn js, can you guys tell me why typeof typeof x returns string, providing code snippet below, if i understand this simple concept it will help me more:
var x=null;
console.log(typeof typeof x);
typeof x returns a string representation of the type of x. So, naturally, typeof typeof x is string.
From MDN:
The typeof operator returns a string indicating the type of the unevaluated operand.
Check this simple example, it will clear your doubt:
var a = null;
console.log(typeof a, typeof a === 'object')
var b = function (){};
console.log(typeof b, typeof b === 'function')
var c = "";
console.log(typeof c, typeof c === 'string')
Reason: typeof returns a string, of the type of the value you provided, When you check the value returned by typeof, it will be in string form, like:
'object', 'function', 'string' etc.
And you are checking the typeof "object", that's why it returned string.
typeof operator to find the data type of a JavaScript variable
// This stands since the beginning of JavaScript
typeof null === 'object';
var x=null;
var x=(typeof x);
it returns "object";
var y=typeof "object";
it returns string
so
console.log(typeof typeof x);
show string

What is the difference between "defined" and defined in javascript?

When writing this in javascript, I have seen it written in two different ways:
if (typeof x === "undefined") {
// execute code here
}
if (typeof x === undefined) {
// execute code here
}
my question here is:
what is the difference between "undefined" and undefined. One is enclosed in quotes and the other is not.
Can anybody clarify this for me?
Thanks!
undefined is a value, 'undefined' is a string literal. The typeof operator returns a string that gives you the type. So typeof x returns the string name of the type of x.
Use if( x === undefined ) or if( typeof x === 'undefined' ) but never if( typeof x === undefined ) because typeof x will always return a string (which will never equal undefined).
"undefined" is a string, and undefined is a variable containing the primitive value undefined (Thank you elclanrs).
if(typeof x === undefined) should only ever be able to return true if undefined is reassigned to a string matching the type of x.

Javascript : Seems like typeof doesn't work

I want to set a Value in a javascript object only when it is not set. My (test) function looks like:
var test = function(){
this.value = {};
this.setValue = function(seperator, newValue){
console.log((this.value[seperator] === "undefined")); //Why both times false?
if(typeof(this.value[seperator] === "undefined")){
this.value[seperator] = newValue;
}else{
//noop
}
console.log(this.value[seperator]);
}
}
var blubb = new test();
blubb .setValue("foo","bar");
blubb .setValue("foo","notme");
in the js console it returns
false
bar
false
notme
Can someone tell me why both time my test of "undefined" told me that is not defined?
thanks in advance
Because undefined in JS is not a string, it's a property of global object and you comparing by type using ===.
=== will compare not only values but their types too:
1 === "1" // false
1 == "1" // true
Try this:
console.log(( typeof this.value[seperator] === "undefined"));
typeof operator transforms variable type to string and only then you can check if your variable is equal to string undefined.
In your second piece of code:
if(typeof(this.value[seperator] === "undefined")){
you use typeof operator outside of the variable so your code first checks if this.value[seperator] === "undefined" then it returns false to you and then you check by "typeof false", it will return boolean for you.
In final step your code converts to:
if( "boolean" ){
And this is always true as string is not empty.
Short answer:
"undefined" !== undefined
Check for undefined instead.
> var foo = { foo: 'foo' };
> foo['bar']
undefined
> typeof(foo['bar'])
"undefined"
Also note that typeof(this.value[seperator] === "undefined") means typeof(boolean) as it'd first evaluate your expression (this.value[seperator] === "undefined") and then get the type of that.
You probably meant typeof(this.value[seperator]) === "undefined".
Your brackets are in the wrong place in this line:
if(typeof(this.value[seperator] === "undefined")){
You're doing the typeof of (this.value[seperator] === "undefined") - that's a boolean condition (will return true or false) so I'd expect typeof to give you "boolean". Then your if statements condition is the string "boolean" which, since it's not zero length, is considered true in JavaScript.
What you wanted is:
if((typeof this.value[seperator]) === "undefined") {

How can I check if variable in java script is type of a particular object

How can I check if variable in java script is type of a particular object? What will be the result of this
var myvalue = "200"+50+44;
1) The typeof operator returns a string indicating the type of the unevaluated operand.
2) The result will be 2005044
I think you're trying like this
parseInt("200", 10)+50+44 // returns 294
Check parseInt(string, radix) for more information.
The type can be checked with the typeof operator.
typeof myvalue === "number"
The possible types are "number", "string", "object", "undefined". This has a few problems though.
typeof someArray === "object"
typeof null === "object"
The better way is compare constructors.
someArray.constructor === Array
someNumber.constructor === Number
You do however need to check if it's null or undefined, because neither have a constructor property.
someThing != null && someThing.constructor === SomeConstructor

Categories