Whisher
Whisher

Reputation: 32766

angularjs core is(type) equals-operator == instead of === i

I'm peeping into angular source and I run into this code

function isObject(value){return value != null && typeof value == 'object';}

I'm wondering if there is a reason because of the use == instead of ===

Thanks in advance

Upvotes: 1

Views: 3438

Answers (2)

Eduardo Serrano
Eduardo Serrano

Reputation: 31

By using = you assign a value to something.

x = 1 // x now equals 1
x = 2 // x now equals 2

By using == you check if something is equal to something else. NOTE: This is not strict.

x == 1 // is x equal to 1? (False)
x == 2 // is x equal to 2? (True)

By using === you check if something is equal to something else. NOTE: This is strict.

x === 1 //is x equal to 1? (False)
x === 2 //is x equal to 2? (True

Strict is that it checks not only the equality of the two values, it compares the types of the two values too. I hope this helps.

Upvotes: 0

Wottensprels
Wottensprels

Reputation: 3327

I would say that it really doesn't matter. typeof will return a string, no matter what

The identically equal and not identically equal operators do the same thing as equal and not equal, except that they do not convert operands before testing for equality.

See:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
JavaScript for professional Webdevelopers

I personally see no reason for using Angulars method instead of typeof, though. Sure, it prevents a false check result if the value is null, but do we need another method for this? Maybe someone can enlighten me.

Upvotes: 1

Related Questions