JavaScript (archaic) if-then-else Syntax Explanation

I wanted to add an explanation for a JavaScript phrase that a couple of you asked me about. When you encounter the a statement that looks like this:testpop = (defwin == null) || typeof(defwin) == “undefined”) ? true : false;

you should view this as you would an IF - THEN - ELSE statement in Visual Basic (or any other programming language).

First, I should explain the ” || ” symbol, which is known as a pipe. You create this by using the backslash key on your keyboard with the shift key down, where on the keyboard, it looks like the symbol I typed in above with a break in the middle for each line. In programming, the pipe stands in for the OR operator. Now that you understand this symbol, we can get down to the syntax of the IF statement above.

I decipher this by starting with the question mark and moving to the left and verbalize it as follows: if defwin is null OR has a value of undefined, then the IF statement is true. If defwin has any other value, the IF statement is false. The result of the IF statement is assigned to the variable testpop (and this is why the equal sign is referred to as an assignment operator in programming).

This type of IF statement is considered ‘old school’ since it derives from old scripting languages. However, you can still use it in .Net and other languages. Now, if you see it referred to elsewhere, you’ll know what it means!

Leave a Reply

You must be logged in to post a comment.