0

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)

Kab
  • 1
  • 3

2 Answers2

1

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)

Gautam
  • 815
  • 6
  • 15
0

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 === '')

Fyllekanin
  • 274
  • 1
  • 12
  • Thank you for the advice. I'll keep it in mind as I attempt to finish the script. – Kab Oct 14 '17 at 17:51
  • OP want's non-null, so they should use `if(variable !== null)` as I stated in the comments. Using `if(variable)` is incorrect. `false`, empty strings, etc, are not `null`. – Spencer Wieczorek Oct 14 '17 at 22:14