Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any function like string.isnullorempty() in javascript

I always (thing != undefined || thing != null)?...:...; check. Is there any method will return bool after this check in javascript or jquery ?

And how would you add this check in jquery as a function?

like image 441
uzay95 Avatar asked Oct 20 '10 12:10

uzay95


People also ask

How check string is null in JavaScript?

Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty. Copied!

What does string IsNullOrEmpty return?

The C# IsNullOrEmpty() method is used to check whether the specified string is null or an Empty string. It returns a boolean value either true or false.

How do you check if a string is empty or undefined in JavaScript?

Say, if a string is empty var name = "" then console. log(! name) returns true . this function will return true if val is empty, null, undefined, false, the number 0 or NaN.

How do I check if a string is empty?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.


2 Answers

if (thing) {    //your code } 

Is that what you are looking for?

like image 68
femseks Avatar answered Oct 07 '22 18:10

femseks


In Javascript, the values null, undefined, "", 0, NaN, and false are all "falsy" and will fail a conditional.
All other values are "truthy" and will pass a conditional.

Therefore, you can simply write thing ? ... : ....

like image 44
SLaks Avatar answered Oct 07 '22 18:10

SLaks