Expressions in JavaScript

JavaScript expressions are very similar to those in PHP, an expression is a combination of values, variables, operators, and functions that results in a value; the result can be a number, a string, or a Boolean value (which evaluates to either true or false).

Following Example shows some simple expressions. For each line, it prints out a letter between a and d, followed by a colon and the result of the expressions (the
tag is there to create a line break and separate the output into four lines).

document.write("a: " + (42 > 3) + "<br />")
document.write("b: " + (91 < 4) + "<br />")
document.write("c: " + (8 == 2) + "<br />")
document.write("d: " + (4 < 17) + "<br />")

In JavaScript, when checking whether a value is true or false, all values evaluate to true with the exception of the following, which evaluate to false: the string false itself, 0, −0, the empty string, null, undefined, and NaN (Not a Number, a computer engineering concept for an illegal floating-point operation such as division by zero).
Note how I am referring to true and false in lowercase. This is because, unlike in PHP, these values must be in lowercase in JavaScript. Therefore only the first of the two following statements will display, printing the lowercase word “true,” because the second will cause a “‘TRUE’ is not defined” error:

if (1 == true) document.write('true') // True
if (1 == TRUE) document.write('TRUE') // Will cause an error

You may also like...