Reputation: 1
I am currently new to javascript and running through some of my textbooks coding projects and the current problem asks to create an if statement that checks the values of the elements referenced by the name fname, lname, and zip are all non-null.
This is the code I have so far for the script
var newAccountArray = [];
function createID() {
var fname = fnameinput;
var lname = lnameinput;
var zip = zipinput;
var account = accountidbox
var fields = input;
var acctid;
var firstInit;
var lastInit;
if ( !== null)
}
I was wondering if I had to do something different for multiple variables
use if (myVar) or if (myVar !== null)
Upvotes: 0
Views: 324
Reputation: 274
As mentioned by Rohitas Behera in the comments if(variable) is enough.
if(variable) will not pass when variable is either of the following: * null * undefined * false (boolean value). * empty string
One common problem with using if(variable) is that you still want the if statement to pass if the variable is an empty string as it's some times a legit case. In these cases you can do.
if(variable || variable === '')
Upvotes: 0
Reputation: 825
You can do something like
if(fname && lname && zip){
// your code
}
This will check for if fname, lname and zip are not (null, undefined, "", false, 0, NaN)
Upvotes: 1