JavaScript Introduction | Operators | Other Operators
String Concatenation Operator
In JavaScript, the addition (+) operator performs two different operations depending on the types of its operands.
- If both operands are numbers, it performs arithmetic addition.
- If at least one operand is a string, it performs string concatenation.
var x = 3 + 4; // If both operands are numbers, addition is performed.
var y = "Have " + "a nice day!" // If both operands are strings, string concatenation is performed.
var z = "December " + 12 // If at least one operand is a string, string concatenation is performed.
When one operand is a string and the other is not, JavaScript automatically converts the non-string operand to a string and then performs string concatenation.
Ternary Operator
The ternary operator is the only conditional operator that has three operands.
The syntax of the ternary operator is as follows.
Syntax
expression ? returnValue1 : returnValue2
Depending on the expression before the question mark (?), if the result is true, returnValue1 is returned; if the result is false, returnValue2 is returned.
var x = 3, y = 5;
var result = (x > y) ? x : y // returns x if x is larger; otherwise returns y.
document.write("The larger of the two numbers is " + result + ".");
The ternary operator can be used instead of a short if / else statement and helps make code concise.
Comma Operator
When the comma operator is used in a for statement, several variables can be updated at the same time in each loop.
// In each loop, i increases by 1 while j decreases by 1.
for (var i = 0, j = 9; i <= j; i++, j--) {
document.write("The value of i is " + i + ", and the value of j is " + j + ".<br>");
}
delete Operator
The delete operator deletes its operand, such as an object, an object property, or an array element.
It returns true when the operand is successfully deleted, and false when it cannot be deleted.
This operator is a unary operator with only one operand, and its associativity is from right to left.
var arr = [1, 2, 3]; // create an array
delete arr[2]; // deletes the element whose index is 2.
document.write(arr + "<br>"); // [1, 2, ]
// An empty slot is created in the array; it is not directly set to undefined.
document.write(arr[2] + "<br>");
// It deletes the array element, but it does not reduce the array length.
document.write(arr.length);
typeof Operator
The typeof operator returns the type of its operand.
This operator is a unary operator with only one operand, and its associativity is from right to left.
The results of the typeof operator for commonly used JavaScript values are as follows.
| Value | Result of the typeof operator |
|---|---|
| Number, NaN | “number” |
| String | “string” |
| true, false | “boolean” |
| null | “object” |
| undefined | “undefined” |
| Function | “function” |
| Non-function object | “object” |
typeof "string" // string
typeof 10 // number
typeof NaN // number
typeof false // boolean
typeof undefined // undefined
typeof new Date() // object
typeof null // object
instanceof Operator
The instanceof operator checks whether its operand object is an instance of a specific object.
It returns true if the operand is an instance of the specified object, and false if it is not.
This operator is a binary operator with two operands, and the associativity of its operands is from left to right.
var str = new String("This is a string.");
str instanceof Object; // true
str instanceof String; // true
str instanceof Array; // false
str instanceof Number; // false
str instanceof Boolean; // false
void Operator
The void operator always returns only the undefined value, regardless of the type of value used as its operand.
This operator is a unary operator with only one operand, and its associativity is from right to left.
<a href="javascript:void(0)">This link does not work.</a>
<a href="javascript:void(document.body.style.backgroundColor='yellow')">
This link also does not work, but it changes the background color of the HTML document.
</a>
As in the example above, the void operator is often used in a form such as void(0) to obtain an undefined primitive value.