This question already has answers here:
What is the difference between null and undefined in JavaScript?
(38 answers)
Why does typeof NaN return 'number'?
(21 answers)
Closed 4 years ago.
What is the difference between Null, NaN and undefined in JavaScript?
I have come across all three values, and have understood them to be “there isn’t anything here” in the context I found them- but I was hoping for a more detailed explanation as to why they occur, as well as what they mean in different contexts (for example- against an array, vs a class or variable).
NaN: Not a number: As the name implies, it is used to denote that the value of an object is not a number. There are many ways that you can generate this error, one being invalid math opertaions such as 0/0 or sqrt(-1)
undefined: It means that the object doesn't have any value, therefore undefined. This occurs when you create a variable and don't assign a value to it.
null: It means that the object is empty and isn't pointing to any memory address.
Related
This question already has answers here:
do loose equality comparision using array.includes
(2 answers)
How does the Array.prototype.includes function compare objects
(5 answers)
Closed 4 months ago.
While writing a sudoku solver, I ran into a issue which took me hours to solve which is based on ['1','2','3'].includes(3). is false. Since 3 in includes is a type number but 3 in array is type string. But this is against the intuition that type coercion happens while comparing values in js? Should it be corrected in js?
ex: ['1','2','3'].includes(3) // false
and ['1','2','3'].includes('3') // true
Throw some light on whether this is needed or should be fixed as it will lead to errors in programs if not checked carefully.
This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Does javascript have a method to replace part of a string without creating a new string?
(4 answers)
Closed 1 year ago.
var a = 'cat' ;
a[0] = 'r' ;
a = 'cat'
Why..??
In case of string although you can access elements by array notation, if you try to change its content it will fail silently i.e. will not throw any error but will not change content either.
Please explain me detail.
Strings are primitive values in javascript, and are therefore immutable. This is just how the language works so there's not much to explain besides that. You can read more about it here!
It is not throwing an error because you're probably not running it in strict mode.
Strings are immutable, in JavaScript only objects and arrays are mutable. You can search for mutable data types in JavaScript in Google.
"A mutable object is an object whose state can be modified after it is created." MDN.
You can read more here: Mutable
This question already has answers here:
Why does typeof NaN return 'number'?
(21 answers)
Closed 5 years ago.
I'm not able to establish if my javascript variable is a number or not.
Here's what I have:
alert('variable startDateB: ' + startDateB);
Results:
variable startDateB: NaN
In the very next line I have:
alert('typeof startDateB: ' + typeof(startDateB));
Results:
typeof startDateB: number
My end goal is to compare this date with other dates, but I don't know whether a conversion is necessary since I appear to be getting mixed information about the variable's data type.
Any help is greatly appreciated!
Thanks!
By definition, NaN is the return value from operations which have an undefined numerical result.
Aside from being part of the global object, it is also part of the Number object: Number.NaN.
This is why you are seeing the behavior you describe. NaN is part of the Number object.
It is still a numeric data type, but it is undefined as a real number.
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 6 years ago.
I was going through Map Documentation on MDN. In Examples, under Using Map Object, Object Literal - {} is used as key to store value. But, the value in Map can't be retrieved using Object Literal.
I verified this in Browser Console and found that Object Literal is not equal to itself. Also, the Function Expression - function() {} is not equal to itself.
I couldn't find the reason behind this. If required, I can ask a different question for Function Expression.
Each time you do {}, it creates a new empty object, so when you do {} == {}, you're comparing two different objects. This comparison is done by reference, so it returns false.
This question already has answers here:
Check if a value is an object in JavaScript
(54 answers)
Closed 7 years ago.
Is it possible to test a variable to see if it is a primitive?
I have seen lots of questions about testing an variable to see if it is an object, but not testing for a primitive.
This question is academic, I don't actually need to perform this test from my own code. I'm just trying to get a deeper understanding of JavaScript.
To test for any primitive:
function isPrimitive(test) {
return test !== Object(test);
}
Example:
isPrimitive(100); // true
isPrimitive(new Number(100)); // false
http://jsfiddle.net/kieranpotts/dy791s96/
Object accepts an argument and returns if it is an object, or returns an object otherwise.
Then, you can use a strict equality comparison, which compares types and values.
If value was an object, Object(value) will be the same object, so value === Object(value). If value wasn't an object, value !== Object(value) because they will have different types.
So you can use
Object(value) !== value