Setting new Date() by number and string gives different result - javascript

I'm learning JavaScript and I found weird(?) behavior of JavaScript.
I create date objects by
var stack = new Date(1404187200000) // 07-01-2014
var overflow = new Date('07-01-2014')
And when I compare those two date objects
stack == overflow // returns false
stack.getTime() == overflow.getTime() // returns true
And I believe it's because they are not the same object. But I know that '==' is comparison of equality and '===' is comparison of identity - like this example:
var stack = 1;
var overflow = '1';
stack == overflow // returns true
stack === overflow // returns false
So, why does comparing new Date([NUMBER]) and new Date([STRING]) give a different result even though they are the same date?
Please enlighten me!

You're misunderstanding the difference between == and ===. It's not that one does equality checking and one does reference checking.
For ===, the two operands have to have the same type. But for ==, type coercion is allowed before checking for equality.
In your case, the two objects are of the same type, so there's no difference between == and ===; but they are checking reference equality, not value. The right way to check for value equality with dates is as you're doing: check whether stack.getTime() == overflow.getTime().
You can also do +stack == +overflow, which will cast them both first, and then you'll get a value equality test.

new Date returns an object. Each time you create it it will create a different object, so they're not equal. getTime returns a value (property) from the object-- this will be the same for both objects.

This is a little bit more complicated. === checks the type while == tries to convert to a common type. That's why 1 == '1' is true but 1 === '1' is false, because in the first case '1' gets transformed to a number (AFAIR).
you can see the exact specification how that is handled here: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 - the interesting part for you in this case is 1. f.
Return true if x and y refer to the same object. Otherwise, return false.

Related

Value is defined but still showing "cannot read the properties of null" (foreach) [duplicate]

I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators

My 2nd If-else conditional is printing the wrong output [duplicate]

I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators

Not getting the third condition [duplicate]

I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators

javascript object comparison: equal vs strict equal

The difference between == and === is that the former one checks values only (1 == "1" will return true) whether the latter one checks the values and additionally checks types (1 === "1" will return false since number is not string).
Comparing objects means comparing object references (object variable holds internal addresses to the objects they refer to and those addresses are being compared). If two objects have totally the same keys and values, functions, etc. but they are separate objects, == will return false so === will also return false.
The question is: does it make any difference if I use == or === comparison operator concerning JavaScript objects? PS if I'm wrong anywhere, please correct me and explain (and I'll accept it as the question answer)
edit: this is NOT about javascript primitives, so comparing objects and primitives is off-topic.
Simple comparison of user-defined objects (I assume you're asking about them and not about primitives such as string and Numbers), never returns true, so there is no point using it. You can check whether two objects are of the same type by comparing their prototypes and constructors, but then again it is indifferent wheter you use == or ===. The only difference is that the comparison may return true. But this of course does not say anything about the properties of an instance, two instances with the same prototype and constructor may have different properties
function cat1 () {
this.name = "blacky";
this.age = 9;
}
function cat2 () {
this.name = "blacky";
this.age = 9;
}
var anton = new cat1()
var john = new cat2()
var kevin = new cat1()
console.log(anton == kevin) // false
console.log(anton == john) // false
console.log(anton === john) // false
console.log(anton === kevin) // false
console.log(anton.__proto__ == kevin.__proto__) // true
console.log(anton.constructor == kevin.constructor) // true
console.log(anton.constructor == john.constructor) // false
console.log(anton.__proto__ == john.__proto__) // false
To conclude then, the answer to your question is: no, it does not make any difference whether you use == or === for comparing objects, because comparing them always returns false. If you want to compare user-defined (not primitive) types you should compare them directly by using proto method of an object which returns the prototype of each object.

How to check if javascript variable exist, empty, array, (array but empty), undefined, object and so on

How to check if javascript variable exist, empty, array, (array but empty), undefined, object and so on
As mentioned in the title, I need a general overview how to check javascript variables in several cases without returning any error causing the browser to stop processing the pageload.
(now I have several issues in this topic.
For example IE stops with error in case _os is undefined, other browsers doesnt:
var _os = fbuser.orders;
var o =0;
var _ret = false;
for (var i = 0; i < _os.length; i++){
...
Furthermore i also need a guide of the proper using operators like == , ===.
As mentioned in the title, I need a general overview how to check
javascript variables in several cases without returning any error
causing the browser to stop processing the pageload.
To check whether or not variables is there, you can simply use typeof:
if (typeof _os != 'undefined'){
// your code
}
The typeof will also help you avoid var undefined error when checking that way.
Furthermore i also need a guide of the proper using operators like ==
, ===.
Both are equality operators. First one does loose comparison to check for values while latter not only checks value but also type of operands being compared.
Here is an example:
4 == "4" // true
4 === "4" // false because types are different eg number and string
With == javascript does type cohersion automatically. When you are sure about type and value of both operands, always use strict equality operator eg ===.
Generally, using typeof is problematic, you should ONLY use it to check whether or not a variables is present.
Read More at MDN:
https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof
The typeof operator is very helpful here:
typeof asdf
// "undefined"
Perhaps you want something like this in your case:
// Handle cases where `fbuser.orders` is not an array:
var _os = fbuser.orders || []

Categories