PROWAREtech

articles » current » javascript » tutorial » page-07

JavaScript: Tutorial - A Guide to JavaScript - Page 07

Language Basics (Operators: Relational, Equality, Conditional, Assignment, Comma).

Relational Operators

The less-than (<), greater-than (>), less-than-or-equal-to (<=), and greater-than-or-equal-to (>=) relational operators perform comparisons between values. Each of these operators returns a Boolean value.

alert(4 < 3); // false
alert(4 > 3); // true

Beware of comparing different data types. Be aware that:

  1. If the operands are numbers then a numeric comparison is performed.
  2. If the operands are strings then the character codes of each string are compared.
  3. If one operand is a number then the other operand is converted to a number and a numeric comparison is performed.
  4. If an operand is an object then call its valueOf() method and use its result to perform the comparison according to the previous rules.
  5. If valueOf() is not available then call toString() and use that value according to the previous rules.
  6. If an operand is a Boolean then it converts to a number and performs the comparison.

String comparisons are case-sensitive. Convert both operands to a common case (either uppercase or lowercase).

var result1 = "SUPERMAN" < "batman";							 // true
alert(result1);
var result2 = "SUPERMAN".toLowerCase() < "batman".toLowerCase(); // false
alert(result2);

Equality Operators

Determining whether two variables are equivalent is fairly straightforward when dealing with strings, numbers, and Boolean values, but when dealing with objects, things become complicated.

Equal, Not Equal, Identically Equal and Not Identically Equal

The equal operator is the double equal sign (==). It returns true if the operands are equal. The not-equal operator is the exclamation point followed by an equal sign (!=). It returns true if two operands are not equal. Both operators do conversions to determine if two operands are equal.

The identically equal operator is the triple equal sign (===). It returns true if the operands are both equal and the same data type. The not-identically equal operator is an exclamation point followed by two equal signs (!==). It returns true if the operands are not equal or not of the same data type.

var numString = "123";
if (numString == 123) {
	// code runs because the if statement is true (they are converted into the same data type)
}
if (numString != 123) {
	// code does not runs because the if statement is false (they are converted into the same data type)
}
if (numString === 123) {
	// code does not run because the if statement is false (they are different data types)
}
if (numString !== 123) {
	// code runs because the if statement is true (they are different data types)
}
alert(numString == 123);
alert(numString != 123);
alert(numString === 123);
alert(numString !== 123);

Conditional Operator

The conditional operator is as follows:

var num1 = 123;
var num2 = 456;
var min = (num1 < num2) ? num1 : num2;
alert(min);

This allows a conditional assignment to a variable depending on the evaluation of the (num1 < num2) boolean expression.

Assignment Operators

Simple assignment is done with the equal sign (=). Compound assignment is done with one of the multiplicative, additive, or bitwise-shift operators followed by an equal sign (=). For example:

var num = 3;	// num is 3
alert(num);
num = num + 10; // num is 13
alert(num);

num += 10;		// num is 23
alert(num);

num = 5;
alert(num);
num %= 3;		 // num is 2
alert(num);

num <<= 2;		// num is 8
alert(num);

num /= 4;		 // num is 2
alert(num);

The Comma Operator

The comma operator allows execution of more than one operation in a single statement.

var num1 = 1, num2 = 2, num3 = 4, num4 = 8;
<<<[Page 7 of 22]>>>

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE