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:
The result:
Try also to other operators with the following code:
Arithmetic Operator
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:
Assignment Operator
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:
Comparison Operator
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:
<html lang="en">
<head>
<title>Logic Operatortitle>
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}
`);
// operator || (or)
var hasil = right || wrong;
document.write(`${right} || ${wrong} = ${hasil}
`);
// operator ! (not)
var hasil = !right
document.write(`!${right} = ${hasil}
`);
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:
<html lang="en">
<head>
<title>Operator Bitwisetitle>
head>
<body>
<script>
var x = 4;
var y = 3;
// operator bitwise and
var hasil = x & y;
document.write(`${x} & ${y} = ${hasil}
`);
// operator bitwise or
var hasil = x | y;
document.write(`${x} | ${y} = ${hasil}
`);
// operator bitwise xor
var hasil = x ^ y;
document.write(`${x} ^ ${y} = ${hasil}
`);
// operator negasi
var hasil = ~x;
document.write(`~${x} = ${hasil}
`);
// operator bitwise right shift >>
var hasil = x >> y;
document.write(`${x} >> ${y} = ${hasil}
`);
// operator bitwise right shift <<
var hasil = x << y;
document.write(`${x} << ${y} = ${hasil}
`);
// operator bitwise right shift (unsigned) >>>
var hasil = x >>> y;
document.write(`${x} >>> ${y} = ${hasil}
`);
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 the middle 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:
? "True : False"
Pay attention! 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:
Ternary Operators
The result: