Javascript: How to check if a string is empty? [duplicate] - javascript

This question already has answers here:
How do I check for an empty/undefined/null string in JavaScript?
(52 answers)
Closed 3 years ago.
I know this is really basic, but I am new to javascript and can't find an answer anywhere.
How can I check if a string is empty?

I check length.
if (str.length == 0) {
}

If you want to know if it's an empty string use === instead of ==.
if(variable === "") {
}
This is because === will only return true if the values on both sides are of the same type, in this case a string.
for example:
(false == "") will return true, and (false === "") will return false.

This should work:
if (variable === "") {
}

But for a better check:
if(str === null || str === '')
{
//enter code here
}

if (value == "") {
// it is empty
}

Related

Why is my JS function always returning true? [duplicate]

This question already has answers here:
Why doesn't my simple if-statement render false in javascript?
(2 answers)
Closed 6 years ago.
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long. Why?
= is always assignment. Equality comparison is == (loose, coerces types to try to make a match) or === (no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '') is that the assignment str = '' is done, and then the resulting value ('') is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since '' is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9) step. Since at that point, str.length is 0, that's the path the code takes.

Why doesn't my equality comparison using = (a single equals) work correctly? [duplicate]

This question already has answers here:
Why doesn't my simple if-statement render false in javascript?
(2 answers)
Closed 6 years ago.
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long. Why?
= is always assignment. Equality comparison is == (loose, coerces types to try to make a match) or === (no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '') is that the assignment str = '' is done, and then the resulting value ('') is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since '' is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9) step. Since at that point, str.length is 0, that's the path the code takes.

Javascript disallow space only in input area

I tried using following method to ban users from leaving only space in the input area with no luck:
method one:
var formSub = $('#formsub').val();
if (formSub == null || formSub == "") {
return false;
}
method two:
if (formSub.trim() == "" || formSub.trim() == " ") {
return false;
}
method three:
if ($.trim(formSub) == "" || $.trim(formSub) == " ") {
return false;
}
Any thought? :)
Use a simple regexp:
/\S/.test(formSub)
where \S refers to any non-white space character.
This removes the dependency on trim (not found in IE<=8) and/or jQuery.
It should be formSub == null || formSub.trim() === "".
=== and == isn't exactly the same. == "" can means true, and any string is "true".
Try:
if (formValue.length === 0 || !formValue.trim()) {
return false;
}
Haha! This is a good one you actually walk around the solution all along.
So here for example, you actually already trimmed all the spaces by using .trim()
if (formSub.trim() == "" || formSub.trim() == " ") {
return false;
}
Correct would be just,
if (formSub == " ") {
return false;
}
This will be the most useful, it passes if someone actually wrote something different from spaces, or other invisible characters ;)
Google "Javascript Regex for more info"
if (/\S/.test(formSub)) {
// String is not empty
}
else{
// String is empty and not usefull
}
Cheers ;) ! +1 Appreciated

Can't make multiple if conditions in JavaScript?

I have absolutely no idea why this is not working. Makes no sense to me.
This returns a "syntax error: parse error":
if ($(this).attr("id") === 'search' || opening = true) return false;
For good measure, I also tried the following, which yielded the same result:
if (1 = 1 && 2 = 2) { return false; }
There are three different operators at play:
=: assignment
==: equality
===: strict equality
= actually modifies a variable, so you shouldn't use it inside if statements. That is, you should use ... || opening == true) instead of ... || opening = true).
In JavaScript = is used to assign values, while == and === are used to compare them.
When you put opening = true in your if statement, you aren't checking if opening is true, you are setting opening to true. Try using == instead.
For example,
var x = 5;
if (x == 10) {
alert("x is 10");
} else {
alert("x isn't 10");
}
will display "x isn't 10", while
var x = 5;
if (x = 10) {
alert("x is 10");
} else {
alert("x isn't 10");
}
will display "x is 10".
the first example should read:
if ($(this).attr("id") === 'search' || opening == true) return false;
and the second:
if (1 == 1 && 2 == 2) { return false; }
Note the double equals (==) sign for logic equals is not the same as the single equals (=) sign, that handles variable assignment.
You have an error in your condition
if ($(this).attr("id") === 'search' || opening = true) return false;
should be
if ($(this).attr("id") === 'search' || opening == true) return false;
the problem is with the equals sign
= is different to ==
the first one is the assignment operator. the second one is for comparison
When you test like this:
opening=true;
What you are really doing is setting opening to the value of true. Use == instead.
Finally, order of operations, even if correct, can get confusing. Put parenthesis around each part of the comparison.
if (($(this).attr("id") === 'search') || (opening == true)) return false;
My guess is that === does not exist.
== is for testing equality
so if ($(this).attr("id") === 'search' || opening == true) return false;
should be if ($(this).attr("id") == 'search' || opening == true) return false;

what is the difference between '!= 'and '!== ' [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Is there a difference between !== and != in PHP?
Javascript === vs == : Does it matter which “equal” operator I use?
In some cases when checking for not equal, I saw using != and in some places i saw !==. Is there any difference in that?
Example:
var x = 10;
if (x != 10) {
//...
}
and
if (x !== 10) {
//...
}
== compares only the value and converts between types to find an equality, === compares the types as well.
== means equal
=== means identical
1 is equal to "1", but not identical, because 1 is an integer and "1" is a string.
They are different in terms of strictness of comparison. !== compares variable types in addition to values.
!== will also check the type (int, string, etc.) while != doesn't.
For more information, see the PHP comparison operator documentation.
The !== is strict not equal: Difference between == and === in JavaScript
The difference is that
== (and !=) compare only the value,
=== (and !==) compare the value and the type.
For example
"1" == 1 returns true
"1" === 1 returns false, because one is a string and the other is an integer
Hope this helps. Cheers

Categories