# Know Existing Operator In Javascript Programming

Once we learned about

**variables**and**data types**in Javascript in the previous material , the next matter to be learned is the**operator**in Javascript.
Here is the basic thing that must be understood in any programming language.

Because we will be much use to perform various operations within the program.

What's operators and functions?

Suppose we have two variables like this:

var a = 2;

var b = 3;

How to count variables a and b?

The answer is by using the plus sign (

**+**).There c = a + b;

The sum of the variables a and b are stored in the variable c.

The plus sign (

**+**) is an**operator**.
In conclusion, so the operator on the programming:

**Operator is a**symbol used to perform operations on a value and variable.

Operators in programming is divided into 6 types:

- Arithmetic operators;
- Assignment Operator (Assignment);
- Operator whose relationships or comparisons;
- Logic operators;
- Operator Bitwise;
- Operator Ternary;

Operators shall in any programming language. 6 types of operators to above must be comprehended.

### First: Arithmetic operator for the Javascript

Arithmetic operator is the operator to perform arithmetic operations like addition, subtraction, division, multiplication, and so on.

Arithmetic operators consist of:

operator name Symbol addition + Reduction - Multiplication * promotion ** division / Remaining Share %

To perform multiplication operations, we use the symbol asterisk / star

**(*)**.
Do not use x, as a symbol of x is not included in the programming operators, x is the character.

Then we use an asterisk for reappointment / double star

**(******)**.
For the division, we use a slash symbol

**(/)**.
Example Code:

<script>

var a = 5;

var b = 3;

// using the addition operator

var c = a + b;

console.log(c);

</script>

The result:

Try also to other operators with the following code:

```
<!DOCTYPE html>
<html lang="en">
<head>
<title>Arithmetic Operator</title>
</head>
<body>
<script>
var a = 15;
var b = 4;
var c = 0;
// subtraction
c=a-b;
document.write (`${a} - ${b}= ${c} <br/>`);
// Multiplication
c=a*b;
document.write (`${a} * ${b} = ${c} <br/>`);
// appointment
c = a ** b;
document.write (`${a} ** ${b} = ${c} <br/>`);
// division
c = a / b;
document.write (`${a} / ${b} = ${c} <br/>`);
// Modulo
c = a% b;
document.write (`${a}% ${b} = ${c} <br/>`);
</script>
</body>
</html>
```

The result:

Try to attention modulo operator (%) and the summation operator (+).

Modulo operator is operator to calculate the remainder for.

Example 4 divided by 2, the remainder is 2.

4 % 2 = 2

### Operator Text Merger

In Javascript, when we will perform the operation on the data type string or text using the summation (+), then what will happen is the incorporation of the word; Not a sum such as type number.

Example:

var a = "14" + "4";

The results will be:

144

Why not 18?

Because of these two numbers is a string data type, consider these two numbers are enclosed in quotation marks.

### Second: Assignments Operator on Javascript

The assignment operator is an operator that is used to assign the task to a variable. Usually used to populate the variable.

Example :

var a = 14;

We give the task a variable to store a value of 14.

Assignment operator consists of:

Operator Name Symbol charging Value = Charging and Additions += Charging and Reduction -= Charging and Multiplication *= Charging and reappointment **= Filling and Distribution /= Charging and Time for %=

The assignment operator as arithmetic operators. The assignment operator is also used to perform arithmetic operations.

Example Code:

totalView var = 12;

// use the assignment operator summation

// add value

totalView += 1;

The result:

totalView variable will be increased by one.

The purpose of totalView + = 1 is like this:

totalView = totalView + 1;

Can be read:

totalView variable content with the sum of the previous totalView with 1.

Especially for the assignment operator are summed and reduced by one, can be abbreviated with the

**++**and**-**for subtraction.
Example :

var a = 2;

a ++;

Then the value of the variable a will be 3.

Then the question:

What is the difference with the assignment operator with arithmetic operators?

Arithmetic operators perform arithmetic operations only, while the assignment operator on duty to perform arithmetic operations and also charging.

Here is an example of coding assignment operator:

```
<!DOCTYPE html>
<html lang="en">
<head>
<title>Assignment Operator</title>
</head>
<body>
<script>
document.write("Initially given a score: <br>");
// charging value
var score=100;
document.write ("score =" +score+ "<br/>");
// charging and adding with 5
score += 5;
document.write ("score plus 5 =" + score + "<br/>");
// charging and subtraction by 2
score -= 2;
document.write ("score minus 2 =" + score + "<br/>");
// charging and multiplying by 2
score *= 2;
document.write ("score multiplied by 2 =" + score + "<br/>");
// charging and sharing with 4
score /= 4;
document.write ("score divided by 4 =" + score + "<br/>");
// charging and appointment with 2
score **= 2;
document.write ("score is raised 2 =" + score + "<br/>");
// charging and modulo with 3
score %= 3;
document.write ("score modulated with 3 =" + score + "<br/>");
</script>
</body>
</html>
```

The result:

### Third: Comparison Operators in Javascript

Relation or comparison operator is an operator used to compare two values.

Comparison operators will generate a boolean value of true and false.

Comparison operator consists of:

Operators Name Symbol Greater than > Smaller < Equal to == or === Not Equal ! = Or! == Larger Equals >= Smaller Equals <= Charging and Time for %=

Example Code:

```
<!DOCTYPE html>
<html lang = "en">
<head>
<title>Comparison Operator</title>
</head>
<body>
<script>
var me = 20;
var you = 19;
// together with
var result = me == you;
document.write (`${me} == ${you} = ${result} <br/>`);
// greater than
var result = me > you;
document.write (`${me} > ${you} = ${result} <br/>`);
// bigger equals
var result = me >= you;
document.write (`${me}> = ${you} = ${result} <br/>`);
// smaller
var result = me < you;
document.write (`${me} < ${you} = ${result} <br />`);
// smaller than it is
var result = me <= you;
document.write (`${me} <= $ {you} = ${result} <br />`);
// not equal to
var result = me != you;
document.write (`${me}!= ${you} = ${result} <br/>`);
</script>
</body>
</html>
```

The result:

The question is:

What is the difference

**==**(two equal with symbols) with**===**(three symbols equal with)?
Comparison using == symbol will only compare values only. While using === will compare the data type as well.

Example :

This is true //

var a = "4" == 4; // -> true

// This will evaluate to false whereas

var b = "4" === 4; // -> false

Why is the value of b is false?

Because of "4" (string) and 4 (integer). A different data type.

### Fourth: Logic operator for Javascript

Logical operators are used to perform operations on two boolean values.

This operator consists of:

Operator Name Symbol Logika AND && Logika OR || Negation / reverse !

Example Code:

```
<!DOCTYPE html>
<html lang="en">
<head>
<title>Logic Operator</title>
</head>
<body>
<script>
var me = 20;
var you = 19;
var right = me > you;
var wrong = me < you;
// operator && (and)
var hasil = right && wrong;
document.write(`${right} && ${wrong} = ${hasil}<br/>`);
// operator || (or)
var hasil = right || wrong;
document.write(`${right} || ${wrong} = ${hasil}<br/>`);
// operator ! (not)
var hasil = !right
document.write(`!${right} = ${hasil}<br/>`);
</script>
</body>
</html>
```

The result:

### Fifth: Bitwise operator for the Javascript

Bitwise operators are used for operations based on bit (binary).

This operator consists of:

Name Symbols in Java AND & OR | XOR ^ Negation / reverse ~ Left Shift << Right Shift >> Left Shift (unsigned) <<< Right Shift (unsigned) >>>

This operator applies to data type int, long, short, char, and byte.

This operator will count from bit-for-bit.

For example, we have a variable a = 60 and b = 13.

When made in binary form, will be like this:

a = 00111100

b = 00001101

(Note the binary numbers, the numbers 0 and 1)

Then, the bitwise operation

Operasi AND

a = 00111100

b = 00001101

a & b = 00001100

Operasi OR

a = 00111100

b = 00001101

a | b = 00111101

Operasi XOR

a = 00111100

b = 00001101

a ^ b = 00110001

Operating NOT (Negation / reverse)

a = 00111100

~ a = 11000011

The concept is similar to the operator for Logic. The difference, Bitwise used for binary.

For more details, please try the following code:

```
<!DOCTYPE html>
<html lang="en">
<head>
<title>Operator Bitwise</title>
</head>
<body>
<script>
var x = 4;
var y = 3;
// operator bitwise and
var hasil = x & y;
document.write(`${x} & ${y} = ${hasil}<br/>`);
// operator bitwise or
var hasil = x | y;
document.write(`${x} | ${y} = ${hasil}<br/>`);
// operator bitwise xor
var hasil = x ^ y;
document.write(`${x} ^ ${y} = ${hasil}<br/>`);
// operator negasi
var hasil = ~x;
document.write(`~${x} = ${hasil}<br/>`);
// operator bitwise right shift >>
var hasil = x >> y;
document.write(`${x} >> ${y} = ${hasil}<br/>`);
// operator bitwise right shift <<
var hasil = x << y;
document.write(`${x} << ${y} = ${hasil}<br/>`);
// operator bitwise right shift (unsigned) >>>
var hasil = x >>> y;
document.write(`${x} >>> ${y} = ${hasil}<br/>`);
</script>
</body>
</html>
```

The result:

### Sixth: Ternary operator whose on Javascript

Recently there Ternary operator.

Ternary operator is an operator that consist of three parts.

Previous operators only two parts, namely: the left and right. This is called a binary operator.

While there trinary operator left, center and right.

the left <operator> the middle <carrier> right part

Operator ternary on Javascript, usually used to create a branching if / else.

Operator ternary symbol consisting of a question mark and a colon (? :).

It looks like this:

<condition>? "True : False"

Pay attention! <Condition> can we fill with an expression that produces a value of true and false.

When the condition is true, then the "right" to be selected and vice versa, when false the "wrong" to be selected.

This operator unique, such as making inquiries.

In the above example, "kamu suka aku" is a question or condition to be checked.

If the answer is true, then yes. Otherwise would not.

More specifically, let's try this coding examples:

```
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ternary Operators</title>
</head>
<body>
<script>
var question = confirm ("Do you accept my feelings?")
var result = question ? "We Are Dating!!" : "You are Crazy!!!";
document.write(result);
</script>
</body>
</html>
```

The result:

## 0 Response to "Know Existing Operator In Javascript Programming"

Post a Comment