Asked  7 Months ago    Answers:  5   Viewed   17 times

Sorry if this is posted a ton but search fields like to repurpose "=" so it's very hard to search for == javascript "== javascript" etc.

But I have some code I'm writing 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?


if ($('#block').css.display=='none') {

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).

== is the comparison operator. It will tell you whether or not two values are equivalent regardless of the type of the operands.

=== 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 checkout: for a quick intro to javascript.

If you prefer to read: 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 be a source.

Tuesday, June 1, 2021
answered 7 Months ago

Putting the onclick within the href would offend those who believe strongly in separation of content from behavior/action. The argument is that your html content should remain focused solely on content, not on presentation or behavior.

The typical path these days is to use a javascript library (eg. jquery) and create an event handler using that library. It would look something like:

$('a').click( function(e) {e.preventDefault(); /*your_code_here;*/ return false; } );
Tuesday, June 1, 2021
answered 7 Months ago

The equality operator will attempt to make the data types the same before making the comparison. On the other hand, the identity operator requires both data types to be the same as a prerequisite.

There are quite a few other posts out there similar to this questions. See:

How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ? (has a nice comparison chart)
Which equals operator (== vs ===) should be used in JavaScript comparisons?

In practice, the identity operator comes in really handy when you want to be certain that a boolean value is true or false since...

1 == true     => true
true == true  => true
1 === true    => false
true === true => true
Wednesday, June 9, 2021
answered 6 Months ago

This is not a bug. Objects do not guarantee any order in their properties and any ordering with Object.keys in some cases should not be depended on.

It is stated in § ECMAScript 5 standard about Object.keys that

If an implementation defines a specific order of enumeration for the for-in statement, that same enumeration order must be used in step 5 of this algorithm.

which implies that any ordering for the for-in statement and Object.keys is entirely implementation-dependent.

Saturday, July 31, 2021
answered 4 Months ago

Since javascript is not a typed languaged any object can be used on logical operators, if this object is null, a false boolean, an empty string, a 0 or an undefined variable then it acts like a false if it's anything else then it is like a true

At the end of the logical operation the last checked value returns.



Check first value -> "6"
6 = true
Go to next value -> "2"
2 = true

End of operation, return last value. 2 which would work the same as true if passed to another logical operation.

Edit: that was a wrong statement. 6||2 returns 6 because 6 acting as true is enough to know the condition OR is true without the need to check the next value.

It is really the same way as in


Check first value -> "true"
Check next value -> "true"
return last value -> "true"

And for 6 && 0 && 2

First value 6 = true
Next value 0 = false

Stop operation here and returns the last checked value: 0.

The | operator is a whole different thing, it simply peforms a logical OR on the bits of the input values, as explaned on the other answer by akp.

Friday, September 24, 2021
Philip Weiser
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :