Reputation: 427
I have written the following code to check if a string is composed of all unique characters
function isUnique(string) {
var charMap = {};
for(var i= 0; i < string.length; i++) {
if (charMap[string[i]] != null) {
charMap[string[i]] = 1;
return false;
} else {
charMap[string[i]] = 0;
}
}
return true;
}
This works when I run it, however, my linter is recommending I use "!==" rather than "!=" to compare to null.
if I change this line to if (charMap[string[i]] !== null) {
, The code stops working and returns false regardless.
If I change this line to if (charMap[string[i]]) {
(which I think should be the same), the function returns true regardless.
Can someone please give a plain text explanation of the differences between these three? I may be making a silly mistake in thinking they are similar so please bear with me.
Upvotes: 0
Views: 526
Reputation: 801
I would use hasOwnProperty
here because this returns a boolean value indicating if the charMap
object has the specified char as the property.
`
function isUnique(string) {
var charMap = {};
for (var i = 0; i < test.length; i++) {
if (charMap.hasOwnProperty(test[i])) {
charMap[test[i]] = 1;
return false;
} else {
charMap[test[i]] = 0;
}
}
return true;
}
`
Upvotes: 0
Reputation: 2856
In the event where a char is not yet in the list, charMap[string[i]]
is undefined, so the comparison works with !=
(because both undefined and null are falsy) but not with !==
(because the type is not the same).
The check for if (charMap[string[i]]) {
doesn't work because you set the value to zero when a char has been found, which is also falsy.
My recommendation is to set the value to 1 if you encounter the char (instead of 0), and then check for charMap[string[i]]!==undefined
or simply charMap[string[i]]
:
function isUnique(string) {
var charMap = {};
for (var i= 0; i < string.length; i++) {
if (charMap[string[i]]) {
return false;
} else {
charMap[string[i]] = 1;
}
}
return true;
}
Upvotes: 0
Reputation: 637
The main difference is that null
and an undefined variable compared with ==
are the same. Compared with ===
, they are not the same.
If I remember correctly, three equals (or one bang and two equals) checks for identity (these things are EXACTLY the same) vs two equals (or one bang and one equals) checks for equality (these things are functionally the same).
You're getting false because you're checking against null
and you're expecting an undefined variable to drop your if to the second branch.
Upvotes: 0
Reputation: 32511
There are two values in JavaScript which are very similar to each other: undefined
and null
.
undefined
is the default state of all variables, including the value of unknown properties in an object.
var x;
console.log(x); // undefined
var obj = {
a: 1
};
console.log(obj.b); // undefined
null
is a value you can assign to something. It's generally used to imply that a value is purposefully non-existent.
When doing a weak comparison against null
(!= null
), it's the equivalent of doing:
x !== null && x !== undefined
By changing your code to
if (charMap[string[i]] !== null) {
you're omitting the check for undefined
, which is what you really wanted in the first place.
Next, you tried
if (charMap[string[i]]) {
This checks to see if the value is "truthy". Basically, it translates to:
x !== false && x !== null && x !== undefined && x !== '' && x !== 0
That last clause is what's catching you. You initialize the value to 0
to start with but your code will never catch that.
Upvotes: 2
Reputation: 67
the first one just compares the value as in the String value, but not comparing the type of the object
The second one will also compare the type of the object.
the third one just checks if it undefined or null.
Upvotes: 0