Sunday, November 18, 2018

JavaScript Operators

Assign values to variables and add them together:

var x = 5;         // assign the value 5 to xvar y = 2;         // assign the value 2 to yvar z = x + y;     // assign the value 7 to z (x + y)

The assignment operator (=) assigns a value to a variable.
var x = 10;

The addition operator (+) adds numbers:
var x = 5;
var y = 2;
var z = x + y;


The multiplication operator (*) multiplies numbers.

var x = 5;
var y = 2;
var z = x * y;



JavaScript Arithmetic Operators


Arithmetic operators are used to perform arithmetic on numbers:


Operator                                           Description
+                                                       Addition
-                                                        Subtraction
*                                                        Multiplication
/                                                         Division
%                                                      Modulus (Division Remainder)
++                                                     Increment
--                                                       Decrement


JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.




Operator                                         Example                                      Same As
=                                                     x = y                                             x = y
+=                                                  x += y                                            x = x + y
-=                                                   x -= y                                             x = x - y
*=                                                  x  *= y                                            x = x * y
/=                                                  x /= y                                               x = x / y
%=                                                x %= y                                             x = x % y


The addition assignment operator (+=) adds a value to a variable.


Assignment


var x = 10;
x += 5;

JavaScript String Operators

The + operator can also be used to add (concatenate) strings.

var txt1 = "John";
var txt2 = "Doe";
var txt3 = txt1 + " " + txt2;

Result of txt3 will be:

---------
John Doe
---------

The += assignment operator can also be used to add (concatenate) strings:

var txt1 = "What a very ";
txt1 += "nice day";

Result of txt1 will be:

--------------------
What a very nice day
--------------------

Adding Strings and Numbers

Adding two numbers, will return the sum, but adding a number and a string will return a string:

var x = 5 + 5;
var y = "5" + 5;
var z = "Hello" + 5;

The result of x, y, and z will be:

10
55
Hello5




JavaScript Comparison Operators

Operator                        Description
==                                 equal to
===                               equal value and equal type
!=                                  not equal
!==                                not equal value or not equal type
>                                   greater than
<                                   less than
>=                                 greater than or equal to
<=                                 less than or equal to
?                                    ternary operator


JavaScript Logical Operators


Operator                       Description
&&                               logical and
||                                   logical or
!                                    logical not

Share:

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

Popular Posts

Blog Archive

Blog Archive

Hassan.mosmer1@gmail.com