Introduction of JS

  • Expression:
    • An expression is a combination of operators and operands that can be evaluated.
    • It may also include function calls which return values.
    • For example – 
      • x=23.5; z=x+y; x=”welcome”;
  • Operators:
    • Operators are symbols which is used to perform some specific operation.

Types of JS Operators

  • JavaScript has many types of operators which can be used according to the need of the program. They are classified based on the functionality they provide during evaluation of the expression.
  • The types of JavaScript operators are –
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Unary Operators
    • Assignment Operator
    • Ternary Operator
    • Bitwise Operators
    • Shift Operators
    • Special Operators
(I) Arithmetic Operators
    • Arithmetic operators are used to perform some specific arithmetic or mathematical operations like addition, subtraction, division, multiplication etc.
    • Arithmetic operators work on one or more numerical values (either literals or variables) and return a single numerical value.
    • The example of basic arithmetic operators are:-

+(Addition),  – (Subtraction),  *(Multiplication),  /(Division), %(Modulus), [**(Power/Exponent)-in ECMAScript, not supported by all browser]

    • For example : 
<script>
var a=10;
var b=20;
var result;
var newline=”<br/>”;
result=a+b;
document.write(“a+b= “);       //console.log(‘a+b= ‘);
document.write(newline);
document.write(result);
</script>
(II) Relational/Comparision Operators
    • Relational operators are some symbols which return a Boolean value either true or false after evaluating the condition.
    • For example : x > y; m<n;
    • Basic JavaScript comparison operators are  :
= = (is equal to) 
=== (strict equal to, when the operands are equal and of the same type)
! = (is not equal to) 
> (is greater than) 
< (is less than) 
< = (is less than or equal to) 
> = (is greater than or equal to)
    • Like Number values, relational operators are functional for strings as well. The comparison takes place in alphabetical order. This alphabetical order is based on ASCII number. For example : 

“zero” < “one”        // false ,because comparision occurs on ASCII value.
“Zero” < “one”       // true
10 < 5                    // false, numeric comparison.
“10” < “5”              // true, string comparison.
“10” < 5                // false, numeric comparison.
“Ten” < 5              // Error occurs, “Ten” can not be converted into a number.

(III) Logical Operators
    • Logical operators are used for combining two or more conditions.
    • JavaScript has following three logical operators :-

&& (AND) : returns true if both operands are true else it return false.
| | (OR) : returns false if both operands are false else it returns true.
! (NOT) : returns true if the operand is false and false if operand is true.

(IV) Unary Operators
    • These operators are used for increasing or decreasing the value of a variable by 1.
    • This operator takes single operand during the work.
    • Calculations performed using these operators are very fast.
    • They are used to increment, decrement or negate a value.
    • These are :-
(a) –  Unary minus, used for negating the values.
(b) ++ Increment operator, used for incrementing the value by 1. There are two varieties of increment operator.

(i) Post-Increment: Value is first used for computing the result and then incremented.
(ii) Pre-Increment: Value is incremented first and then result is computed.

(c) —  Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operator.

(i) Post-decrement: Value is first used for computing the result and then decremented.
(ii) Pre-Decrement: Value is decremented first and then result is computed.

(V) Assignment Operators
    • It assigns the value of its right operand to its left operand.
    • This operator is represented by equal sign(=).
    • For example : x = 50;
    • In many cases, assignment operator can be combined with other operators to build a shorter version of statement called Compound Statement.

For example, instead of a = a+5, we can also write a += 5 as compound statement.
(a) +=: for adding left operand with right operand and then assigning it to variable on the left. for example – int a = 5; a += 5;     //a = a+5;
(b) -=: for subtracting left operand with right operand and then assigning it to variable on the left. for example – int a = 5; a -= 5;     //a = a-5;
(c) *=: for multiplying left operand with right operand and then assigning it to variable on the left. for example – int a = 5; a *= 5;     //a = a*5;
(d) /=: for dividing left operand with right operand and then assigning it to variable on the left.  for example – int a = 5; a /= 5;     //a = a/5;
(e) %=: for assigning modulo of left operand with right operand and then assigning it to variable on the left. for example – int a = 5; a %= 5;     //a = a%5;

(VI) Ternary Operators
    • This conditional operator is a special JavaScript operator that takes three operands. Hence, it is also called ternary operator.
    • A conditional operator assigns a value(either true or false value) to the assigned variable based on the condition.
    • Syntax : var_name = (condition) ? tval_1 : fval_2
      when (condition) is true on evaluation, the value tval_1 is assigned to the variable var_name, otherwise, it assigns the value fval_2 to the variable var_name. 
    • Example : 
info= (age >= 18) ? “adult” : “minor”;

The info variable assigns the value “adult” when true i.e. the age is eighteen or more. Otherwise, it assigns the value “minor” to info variable.

(VII) Concatenation/String Operators
    • When + operator is used with strings, it performs concatenation and combines the string operands as one. However, when + is used with numbers, it performs addition.
    • The + operator gives priority to string operands over numeric operands.
    • It concatenates/works from left to right but the results depend on the order in which operations are performed.
    • For example :-

“Welcome” + “2023” = Welcome2023
“50” + “100” = 50100
“Robert” + “007 “= Robert007
4 + 7 + “Mumbai” =11Mumbai
“Mumbai” + 0 +0+7 =Mumbai7

(VII) Bitwise Operators
    • These operators are used to perform manipulation at individual bits level of a data value/number.
    • They can be used with any of the integer types.
    • They are used when performing update and query operations of Binary indexed tree.
    • Bitwise operators are –

(i) & (Bitwise AND operator): returns bit by bit AND of input values.
(ii) | (Bitwise OR operator): returns bit by bit OR of input values.
(iii) ^ (Bitwise XOR operator): returns bit by bit XOR of input values.
(iv) ~ (Bitwise Not/Complement Operator): This is a unary operator which returns the one’s compliment representation of the input value, i.e. with all bits inversed.

    • For example :
<script>
var a = 2;     // Bit value= 00000010
var b = 3;    // Bit value=  00000011
var newline= “<br />”;
document.write(“(The result of (a & b) is = “);
result = (a & b);
document.write(result);
document.write(newline);
document.write(“The result of (a || b) is = “);
result = (a || b);
document.write(result);
document.write(linebreak);
document.write(“The result of (~b) is = “);
result = (~b);
document.write(result);
document.write(newline);
document.write(“The result of (a << b) is = “);
result = (a << b);
document.write(result);
document.write(newline);
document.write(“The result of (a >> b) is = “);
result = (a >> b);
document.write(result);
document.write(newline);
</script>
(VIII) Shift Operators
    • This operator is used to perform manipulation at individual bits level by shifting the bit value left or right and then results a new data value/number.
    • These are – 

<< (Left Shift), >> (Sign Propagating Right Shift) and >>>(Zero Fill Right Shift)

(IX) Special Operators
  • New Operator
    • New operator is used to create an instance and allocate memory to a user-defined or predefined object types.
    • Syntax :
      ObjectName = new objectType;
    • Example :
      dt = new Date();      // date value assigns to object dt.
      r = new rectangle(4, 5, 7, 8); // pass four values as parameter.
  • Delete Operator
    • The delete operator does the reverse of New operator i.e. de-allocates (releases) the created memory space that was allocated when using the new operator.
    • During de-allocation, the object or object’s element or property was removed.
    • Syntax :

delete object_name;
delete object_name.property;
delete array_name[index];

    • Delete operator can be used to delete variables declared implicitly but not those declared with the var statement.
    • The delete operator returns true if the operation is possible; otherwise it returns false if the operation is not possible.
      x=20;
      var y= 60;
      obj=new Number();
      obj.h=70; // create property h
delete x ; /* returns true (x is declared implicitly, without using var)*/
delete y;  /* returns false ( y is declared explicitly using var) */
delete Math.PI;  /* returns false (cannot delete predefined properties)*/
delete obj.h /* returns true (can delete user-defined properties)*/
delete obj;  /* returns true (can delete if declared implicitly) */
    • When we delete an array element from an array then that element is no longer in the array but the array length will not be affected. For example, suppose if we delete a[5], then a[7] then the a[7] and a[5] location will still remains and will be undefined. 
  • Comma Operator
    • This operator evaluates multiple operands and returns the value of the last operand.
    • For example – let x = (10, 70 , 05);      //  Output =05
  • Void Operator
    • This operator discards or avoids to accept the expression’s return value.
    • void is a unary operator that appears before its single operand, which may be of any data type.
    • For example – void(x);
  • in Operator
    • This operator returns true if the specified property is(present) in the object.
    • For example – x in object;     // Here x is property.
  • instanceof Operator
    • This operator returns  true if the specified object is of of the specified object type.
    • For example – object instanceof object_type;
  • typeof Operator 
    • The typeof operator is a unary nature of operator that contains single operand of any data type.
    • Its output value is string indicating the data type of the operand.
    • It returns true or false value on evaluation.
    • For example : 
<script>
var a = 50;
var b = “Welcome”;
var newline= “<br />”;
result = (typeof a);
document.write(“Result =  “);
document.write(result);
document.write(linebreak);
result = (typeof b);
document.write(“Result =  “);
document.write(result);
document.write(linebreak);
</script>
  • this Operator
    • JavaScript supports ‘this’ operator.
    • The word ‘this’ refers to the current object. It is like a pointer to the current object.
    • Syntax :

this[.propertyName];

    • For example :
<HTML>
	<HEAD>
		<SCRIPT>
			function validate(obj, min_age, max_age)
			{
			 if ((obj.value < min_age) || (obj.value > max_age))
			 alert(“Invalid Job Age ”);
			}
		</script>
	</HEAD>
	<BODY>
		<B>Enter the age from 18 to 40:</B>
		<INPUT TYPE = “text” NAME = “age” SIZE = 2 onChange=”validate(this, 18, 40)”>
	</BODY>
</HTML>

NB: Here, this operator is used to pass current object (i.e. text box value).

Operator Precedence of JS

  • Operators are evaluated in a predefined order of precedence/priority. The following list shows operators from highest priority to lowest priority –

* Multiplication (Highest priorty and executed first)
/ Division
% Modulus
+ Addition
– Subtraction
< Less than
<= Less than equal to
> Greater than
>= Greater than equal to
== Equality
!= Not equality
&& Logical AND
|| Logical OR
?: Conditional
=
+=
–= Assignment Operators
*=
/=
%=
, Comma(Lowest priorty and executed last)

Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.