Javascript check string empty?

Mihai B.

Well-known member
Registered
Joined
Apr 19, 2016
Messages
243
Points
18
Is there a universal JavaScript function that checks that a string is empty or has a value?
I can do this with PHP but with JavaScript, I am new to it.
 

kevinhng86

Member
Registered
Joined
Jan 12, 2017
Messages
58
Points
0
Hi Mihai,

In Javascript there is a lot of way to check on a variable that is native to the program. However you have to utilise them in a way so they can produce something useful in your code. The below is the code example that will check all type of thing for you. The only thing missing is the isNaN() check. isNaN will check if it is Not a Number or not and will return true or false. Try the function below out. It will annoy you a bit from pop but I'm sure it will show you the basic of checking a variables. You can utilise some method from the below function into very complex task for example to validate user input. However this is the basic structure of it.

Code:
var a = {};
// && = and , || = or , ! = not , === = absolute equal to , == = equal to but does not need to be absolute (this one is very complicated to use, I can't explain its all here, just don't use this one if you do not really have to.)
// =  = declaring a variable, !== = not equal to.

function check(input){
	if ( input ===  null || input === undefined ){
    	alert("Variables wasn't defined");
    }
    
    if ( input.length === 0 ){
    	alert("This is a declared but empty variable");
    }

 	if ( !input ){
		alert("I'm empty, false, NaN, 0, undefined or empty");
    }
    
  	alert( typeof(input) ); // This will tell you what type I am.

	if ( typeof(input) === "object" && input instanceof Object && input instanceof Array ){
    	alert( "I'm an Array, which is also an Object in Javascript" );
        alert( "I can be an Object and not be an Array, however if am an Array I am also a class of object") ;
    	alert( "Notice how typeof tell you I'm an Object although I am an Array" );
    }
    
    if ( input instanceof Object && !(input instanceof Array) ){
    	alert( "I am for sure an Object but not an Array" );
    }

}

check(a)
 

Mihai B.

Well-known member
Registered
Joined
Apr 19, 2016
Messages
243
Points
18
Mihai B.
You codes are awesome, I could check it and it returned when I have a string is empty.

var a ="";
check(a);

It alerted: This is a declared but empty variable

var a ="my string";
check(a);

It alerted: string ???

I don't see this alert in your code, can I change this text to a new name?
 

energizedit

Well-known member
Registered
Joined
Dec 13, 2016
Messages
259
Points
18
If you just want to check whether there's any value, you can do


Code:
if (strValue) {
    //do something
}
 

RDO Servers

Well-known member
Registered
Joined
Apr 3, 2015
Messages
1,027
Points
83
RDO Servers
I don't claim to be a pro at JavaScript, but I don't think that will work the way you expect.

That will "//do something" if "strValue" has a value, but not if strValue has a value ==false.
 

myvirtualst1008

Member
Registered
Joined
Jun 2, 2017
Messages
15
Points
0
check for empty strings (""), null, undefined, false and the number 0 and Nan. 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.
 
Older Threads
Latest Threads
Recommended Threads
Replies
1
Views
3,293
Replies
5
Views
3,105
Replies
5
Views
2,344
fwh
Replies
4
Views
4,329

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top