id.length==0 || id=="" Don't you think both mean the same thing?

Asked 1 years ago, Updated 1 years ago, 348 views

id.length==0 || id==""

If you check id.length==0 || id=="" in javascript like this, right?

But doesn't both right and left mean the same thing? If lngth is 0, of course nothing has been entered, so I think it's the same as "

I don't know why I have to write both.

javascript

2023-01-24 03:56

1 Answers

I think it's my first time seeing the code written like you said.
I don't know the intention, but I don't think it's a good code either.
To see if it is an empty string or not, just compare it with ".

Also, id.length == 0 and id == " do not have the same meaning.
For example, if id is 0, id.length is undefined, so the result of id.length==0 is false and id=="/code> is the result of is the result of undefined.

const id = 0;
console.log(id.length == 0); // false
console.log(id == ""); // true

Of course, if you assume that id is a string, the result will be the same.

There are several reasons why I don't think this code is good:


2023-01-24 10:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.