Operators in JavaScript - ProgrammerTech
Language:

Search

Operators in JavaScript

  • Share this:
Operators in JavaScript

Math operators in JavaScript

Operators In JavaScript, you can handle arithmetic operations in JavaScript as easily as any other language. Operators are symbols, and each symbol has a specific meaning. The arithmetic operations in JavaScript are divided into two parts:

  1. Common normal arithmetic operations.
  2. Arithmetic operations on forces and fractions.

 

Common arithmetic operations

These operations are the common operations that everyone knows such as (addition - subtraction - multiplication - division).

  1. For addition we use the + sign.
  2. Subtraction we use the - sign.
  3. Multiplication we use the sign *.
  4. For division we use the / sign.

 

Examples of JavaScript Arithmetic Operations

console.log(23 + 3);
console.log(22 - 2);
console.log(22 / 2);
console.log(23 * 3);

We rendered data for all data via console and the operations in the example were:

  • The first operation is the addition operation 23+3.
  • The second operation is the subtraction process 22 - 2.
  • The third operation is the 22/2 division operation.
  • The fourth operation is 23*3 multiplication.

When we run the previous example in the browser and go to the console, we will find the results as follows:

Example output of arithmetic operations in javascript

 

Example 1

var firstName = "Saja";
var lastName = " Alsadig";
var name = firstName + lastName;
console.log(name);

We created two text variables, the first name and the second name, and assigned them values. Then we created a text variable name and set it to be equal to the sum of the first and second variables. And we displayed the data through the console, and when we run the code in the browser, we find its result:

Combine text in javascript arithmetic operations

 

Arithmetic operations on powers and fractions

This section is mainly focused on fractures and forces as they are rarely used.

  • To double the power means the exponent we use **.
  • To display the remainder of the division we use %.
  • To increment by one we use ++.
  • To decrease by one we use -- .

 

Examples of arithmetic operations on powers and fractions

var number = 2;
console.log(10**8);
console.log(++number);
console.log(--number);

We created a scalar variable number and its value is 2. Then we displayed the data through the console, and the display of this data was as follows.

  • Doubling powers 10*8.
  • Increment by 1 in ++number.
  • Decrease by 1 in --number.

When we run the previous example in the browser and go to the console, we will find the results as follows:

Arithmetic operations on powers and fractions in javascript

 

We notice in the result that he gave us the number 100000000 because it is the result of the 10 to the 8th power, and below the 3, 2 are the result of an increase and decrease of one for the variable var number=2. There are many operations to deal with arithmetic operations shown in the following image. You can create examples from your mind and practice them by yourself.

Increments and decrements in javascript

 

Logical Operators JavaScript

There are many types of Boolean operators that are used in JavaScript and these operators are often used with conditions.

  • The and operator is denoted as && so that it returns true if both operands are true, otherwise it will return false.
  • The or operator is represented by the form || So that it returns true if one of the parameters is true, otherwise it returns false.
  • The not operator is symbolized by the form ! So that it returns true if the parameter is false and returns false if the parameter is true.

 

The logical operator && in JavaScript

Returns true if both parameters are true, otherwise false.

var a = true;
var b = false;
if (a == true && b == false){
    alert("Condition fulfilled");
}
else{
    alert("Condition not met");
}

We created two variables:

  1. The variable a is true.
  2. The variable b has a value of false.

And we set a condition that if the value of the variable a is true and the value of b is false. That is, if the condition is met, print the condition is met, and if it is incorrect, print the condition is not met. And displaying the data was via a pop-up dialer, and since the condition is met, the result in the browser will be:

Boolean && operator in JavaScript

 

We conclude in && that if one is true and the other is false, the condition will not be executed.

 

Boolean || operator in javascript

Returns true if one of the parameters is true, otherwise it will return false.

var a = true;
var b = false;
if (a == true || b == false){
    alert("Condition fulfilled");
}
else{
    alert("Condition not met");
}

We created two variables:

  • The variable a is true.
  • The variable b has a value of false.

And we set a condition if the value of the variable a is true or the value of b is false. If the condition is true, print the condition is met, and if it is incorrect, print the condition is not met. And the data display was via a pop-up dialer, and since the condition is met, the result will be fulfilled. We conclude in || If one of them is true and the other is not, then the condition is fulfilled. I mean, we told the function that this is equal to this or this is equal to this and one of them is true.

 

Boolean ! in JavaScript

Returns true if the parameter is false and returns false if the parameter is true, that is, it reverses the result.

var a = true;
var b = false;
if (!a == true){
    alert("Condition fulfilled");
}
else{
    alert("Condition not met");
}

We conclude that if the value of the condition is true, the condition will not be fulfilled, and if it is false, it will be met.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com