Blurr.P
Blurr.P

Reputation: 35

javascript the difference between the number 01 and 1

I am writing a function that needs to be able to tell the difference between the numbers 01 and 1. I am doing a cubical search within a company directory and on the same floor there are cubicles numbered 01 and 1. When the cubical search returns 01 it automatically assumes the value is a 1 and not '01'.

Is there a method or something I could use to differentiate between '01' and '1'.

Thanks.

Upvotes: 1

Views: 2627

Answers (4)

Olu Adabonyan
Olu Adabonyan

Reputation: 93

You will treat both inout as strings. Consider this:

var x = "01";
var y = "1";
var z;
if (x === y) {
    return true;
} else {
    return false;
}

Note there is a difference between == (value only) and === (value and type)

Upvotes: 0

Quentin
Quentin

Reputation: 944441

There is no difference between the numbers 01 and 1. They are absolutely identical.

console.log(01 === 1);

There is a difference between the strings "01" and "1". If you need to distinguish between the values, then use strings, not numbers.

console.log("01" === "1");

Upvotes: 2

Old Bald Guy
Old Bald Guy

Reputation: 173

These aren't really NUMBERS, they are CHARACTER STRINGS, whose characters all happen to be digits.

I presume that you are doing a "cubicle search" - a search for a cubicle (those little pens that companies keep people in), rather than some sort of mathematical search that involve cubes

Upvotes: 0

Jamiec
Jamiec

Reputation: 136174

Is there a method or something I could use to differentiate between '01' and '1'.

Yes, string comparison.

When you treat these two values as strings then these two values will be different

var isEqual = '1' === '01'; // false

Only by converting them to numbers will they evaluate the same and be indistinguishable.

Upvotes: 0

Related Questions