JavaScript. Special operators

Special operators


Contents


Search other resources:

1. The list of special operators

In JavaScript, special operators perform all sorts of important functions. These operators include:

  • conditional operator ?:. This operator is also called a ternary operator because it requires three operands to produce a result;
  • operator delete;
  • operator in;
  • operator instanceof;
  • operator new;
  • operator this;
  • operator typeof;
  • operator void;
  • operator ‘ , ‘ (comma).

 

2. Ternary operator ? :

Ternary operator ?: uses three operands to produce a result. General form of using the operator

res = condition ? expression1 : expression2

here

  • condition – conditional expression, the result of which is true or false;
  • expression1 – expression to be calculated if condition = true;
  • expression2 – expression to be calculated if condition = false;
  • res is the result obtained as a result of evaluating one of the expressions expression1 or expression2.

The ternary operator ?: can be replaced by an if-else statement.

Example.

// Ternary operator ?:
// 1. Calculate the maximum of two numbers x, y
var x, y
var res1
x = 8
y = 5
res1 = (x > y) ? x : y // res1 = x = 8

// 2. Get notification about string equality/inequality
var s1 = "abcd"
var s2 = "abcde"
var res2 = (s1 == s2) ? "Strings are equal." : "Strings are not equal"

 

3. Operator in

The in operator is intended to determine whether a given value exists in an object. General form of using the operator

value in obj

here

  • obj – an object;
  • value – the value to check for in the obj object.

The result of the operator’s work can be true or false.

Example.

// Operator in
// Checking if the Days array contains an element with index 5
var Days = [ "Monday", "Tuesday", "Wednesday", "Thursday", 
    "Friday", "Saturday", "Sunday" ];

if (5 in Days) {
  document.write("Index 5 is in the array Days.")
}
else
{
  document.write("Index 5 is not in the array Days.")
}

 

4. Operator instanceof

The instanceof operator allows you to determine whether the specified object is of a given type. General form of using the operator

obj instanceof type

here

  • obj – an instance (object) of some type;
  • type – any type (Number, String, …).

Example.

// Operator instanceof
'use strict';

// 1. String type
// Create an object of type String
var str = new String()

if (str instanceof String)
  document.write("str is of type String")
else
  document.write("str is not of type String")

// 2. The object of type Number
var x = Number(10)

if (x instanceof Number)
  document.write("x is of type Number")
else
  document.write("x is not of type Number")

 

5. Operator new. Create an object instance

The new operator is for creating an instance of an object. The operator has the following general usage form

obj = new Type()

here

  • Type – some type. It can be one of the standard types (Number, String, and others) or a type defined by programmer;
  • obj is the name of the object for which the instance is being created.

Example.

// Operator new
// Create an object of type Number
var num = new Number(23)

// Create an object of type String
var str
str = new String("Hello")

 

6. Operator this. Get the current object

The use of the this keyword depends on the context in which it is used. A detailed overview of the use of the this keyword in programs is beyond the scope of this topic.

This topic discusses the case of using this object to obtain a reference to this object. For this case, the using of this operator is as follows

this.item

Here item is some element of the object (variable, object, function, etc.).

Example.

'use strict';

var Point = {
  x: 10,
  y: 10,

  PrintX: function () {
    console.log("x = ", this.x) // this - a reference to a Point object
  },

  PrintY: function () {
    console.log("y = ", this.y) // this - a reference to a Point object
  },

  PrintXY: function () {
    this.PrintX() // call methods of current object
    this.PrintY()
  }
}

var x = 5
Point.PrintX() // x = 10

var y = 55
Point.PrintY() // y = 10

Point.PrintXY()

Program result

x = 10
y = 10
x = 10
y = 10

 

7. Operator typeof. Get the type as a string

Using the typeof operator, you can get the name of an object’s type as a string. General form of using the operator

typeName = typeof(obj)

here

  • obj – an object;
  • typeName – a string type variable containing the type name of the obj object.

Example.

'use strict';

// typeof operator - get a string representation of a type
var Line = {
  x1: 10,
  y1: 10,
  x2: 20,
  y2: 25,

  Length: function () {
    return len = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  }
}

var typeName = typeof(Line) // typeName = "object"
console.log(typeName)

var t = 77
typeName = typeof (t) // typeName = "number"
console.log(typeName)

var s = "Hello!"
typeName = typeof (s) // typeName = "string"
console.log(typeName)

Program result

object
number
string

 

8. Operator void. Perform calculation without returning a value

Evaluation of an operand expression without returning a value is performed by the void operator. The void operator evaluates the given expression and returns undefined. The general form of using the operator

void expression

here

  • expression – expression or function.

Example.

The example shows the use of the void operator for a function and an expression.

'use strict';

// Operator void
// 1. Using void with function
// Function that does not return anything
void function Hello() {
  console.log("Hello, world!")
}()

// 2. Using void with an expression
var b = void (2 == '2')   // b = undefined
console.log("b = ", b)

var res = void (25 * 3)   // res = undefined
console.log("void (25*3) => ", res)

res = void 25 * 3   // res = NaN
console.log("void 25*3 => ", res)

Program result

Hello, world!
b = undefined
void (25*3) => undefined
void 25*3 => NaN

 

9. Operator ‘ , ‘ (comma). Initialization of multiple variables, multiple assignments

The ‘ , ‘ operator is used to perform multiple assignments, other operations within loops, or initialization of variables.

Example.

'use strict';

// Operator , (comma)
// 1. Initialization of variables
var x = 5, y = 8
console.log("x = ", x, "; y = ", y)

// 2. Initializing an array with values
var A = [2, 3, 3, 4]
console.log("A = ", A)

// 3. Several assignments
var a, b, c
a = 3, b = 7, c = 11
console.log("a = ", a, ", b = ", b, ", c = ", c)

// 4. Using a comma in a loop
// Output the squares of numbers from 1 to 5 in the original way
for (var i = 1, j = 1; i <= 5, j <= 5; i++, j++)
  console.log(i * j)

Result

x = 5 ; y = 8
A = [ 2, 3, 3, 4 ]
a = 3 , b = 7 , c =   11
1
4
9
16
25

 


Related topics