This question already has answers here:
Why does ("foo" === new String("foo")) evaluate to false in JavaScript?
(5 answers)
Closed 6 years ago.
I've looked all the questions and answers on stackoverflow, but couldn't find the simple answer to this.
What is exactly the difference between string and object?
For example, if I have this code:
var a = 'Tim';
var b = new String('Tim');
What exactly is the difference?
I understand that new complicates the code, and new String slows it down.
Also, I understand a==b is true, but going more strictly a===b is false. Why?
I seem to fail to understand the process behind the object and string creation.
For example:
var a = new String ('Tim');
var b = new String ('Tim');
a==b is false
a is of type string, whereas b is of type object.
=== includes typechecking and cause string is not an object
a === b will give you a false
new String ('Tim') === new String ('Tim') will evaluate to false too, because both are different objects
For normal strings there is no need to create an object, just create your variable and assign it a value.
And as far as your question regarding why == is true and === is false it's because:
== Compares values
=== Compares values AND type (One is a string, one is an object).
Another example of this is:
var a = 1;
var b = '1';
a == b //True as they both have the same value
a === b //false as one is a string and one is an integer
You can do the following to see the difference:
var a = "foo";
var b = new String("foo");
console.log(a);
console.log(b);
The first one is a string literal and the second is a String object. That's why when you compare them they are not equal but when you compare their values they are. You can read more about literals here.
Related
This question already has answers here:
What is the difference between null and undefined in JavaScript?
(38 answers)
JavaScript checking for null vs. undefined and difference between == and ===
(8 answers)
Closed 4 years ago.
In below code snippet a==b return true, i.e they point to same memory location, hence they will have same value.
I would like to know, how JS engine knows a===b is false.
How is type information determined when 2 different types point to same memory location?
Edit 1: From comments it looks like my question might not be clear. I totally understand difference between == and === in terms of usage in JS language. I am more interested in knowing how JS engine saves type information for null and undefined. As per my understanding variables a & b point to same memory location that is why I get a==b, if this understanding is wrong, please correct me.
Edit 2: Ok I will put my question in another way. How typeof operator knows a is object and b is undefined despite having a==b.
var a = null;
var b = undefined;
console.log(a==b);
console.log(a===b);
console.log(typeof a);
console.log(typeof b);
In the code snippet added in the question:
var a = null; var b = undefined;
console.log(a==b);
console.log(a===b);
console.log(a==b) returns true because == uses type coercion to check the equality of both the variables. Therefore, null and undefined are thought of as equals.
console.log(a===b) returns false because === does not use type coercion. For ===, null and undefined are not same types and it doesn't care about checking deep equality when the operands aren't of the same type.
This has got nothing to do with memory locations.
a = null, b= undefined;
a == b /* only check their values */
a === b /* also check their types + values */
typeof a == typeof b // false
typeof "variable" gives the type of the variable.
This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
Closed 4 years ago.
When you comparing object with another object with same property why it returns false?
For Example
var person={
age:30
}
var person2={
age:40
}
console.log(person==person) or console.log(person===person)
it show's in console false why?
Objects are reference types, which means the equality operators operate on a reference to the object in memory, not to its contents.
In your particular case, you could serialize the object to a string and then check
const compareSerializableObjects = (a, b) =>
JSON.stringify(a) === JSON.stringify(b)
person === person will always return true as you are comparing the same reference, and if you are comparing person === person2 then it is a different refference which is false.
Did you mean person.age === person2.age ?
This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Closed 7 years ago.
I was checking in my console. When i checked empty object directly like {} == {} , it show false.
Why it's showing false ? shouldn't it show true as both are equal ?
When i checked empty object directly like {} == {} , it show false.
You have two different objects here and not one. Here == checks whether the given two objects are same or not.
Scenario 1:
var foo = {}; //new object
var bar = foo; //shared same object
foo == bar;// true
Scenario 2:
var foo = {}; //new object
var bar = {}; //new object
foo == bar;// false
If you still wish to compare two different objects, try this:
var foo = {}; //new object
var bar = {}; //new object
JSON.stringify(foo) == JSON.stringify(bar);// true
Here, JSON.stringify({}) gives string value "{}"
Both are NOT equal. Although they are empty, both refers to different objects.
Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference. That comparison by reference basically checks to see if the objects given refer to the same location in memory, which they do not, so the comparison is false.
This question already has answers here:
Javascript assigning value to primitive
(2 answers)
Closed 8 years ago.
Consider this excerpt from a node.js REPL session:
> var a = 5;
undefined
> a.b = true;
true
> a.b;
undefined
The intention is to store extra properties in a, which if successful would make the final line be true instead of undefined. How can I do this?
You cannot do that. A number is not an object in JavaScript. When you write:
a.b = true;
what happens is that the runtime instantiates a Number instance automatically. That's an object. You set the "b" property on that object to true, and then it's forgotten.
Now, you can do this:
var a = new Number(5);
a.b = true;
alert(a.b);
and it'll work just fine.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
myVar = !!someOtherVar
What does the !! operator (double exclamation point) mean in JavaScript?
Came across this line of code
strict = !!argStrict
... here and wondered what effect the !! has on the line? Pretty new to JS!
It converts your value to a boolean type:
var x = '1';
var y = !!x;
// (typeof y === 'boolean')
Also note the following:
var x = 0;
var y = '0'; // non empty string is truthy
var z = '';
console.log(!!x); // false
console.log(!!y); // true
console.log(!!z); // false
It converts the value to a value of the boolean type by negating it twice. It's used when you want to make sure that a value is a boolean value, and not a value of another type.
In JS everything that deals with booleans accepts values of other types, and some can even return non-booleans (for instance, || and &&). ! however always returns a boolean value so it can be used to convert things to boolean.
It is a pair of logical not operators.
It converts a falsey value (such as 0 or false) to true and then false and a truthy value (such as true or "hello") to false and then true.
The net result is you get a boolean version of whatever the value is.
It converts to boolean
Its a "not not" arg
commonly used to convert (shortcut) string values to bool
like this..
if(!!'true') { alert('its true')}