Which of the following is arithmetic operator in javascript?

Last update on August 19 2022 21:50:38 [UTC/GMT +8 hours]

Arithmetic Operators : +, -, *, /

In JavaScript, arithmetic operators take numerical values [either literals or variables] as their operands and return a single numerical value. There are four standard  arithmetic operators, addition [+], subtraction [-], multiplication [*], and division [/].

These operators work as they do in other programming languages except the division [/] operator which returns a floating-point division in JavaScript, not a truncated division as it does in languages such as C or Java.

  For example:
1/2  returns 0.5 in JavaScript.
1/2  returns 0 in Java.

In addition, JavaScript provides modules[%], increment[++], decrement[--] and unary negation[-] operators.

Example : [ + addition operator]

HTML Code:




JavaScript + operator example







JS Code:

var var1 = 45;
var var2 = 78;
var var3 = 45.10;
var var4 = 178.12;
var newvar  = var1 + var2;
var newvar1 = var3 + var4;
var newParagraph3 = document.createElement["p"];
var newText3 = document.createTextNode["var1 + var2 = "+newvar+" and var3 + var4 = "+newvar1];
newParagraph3.appendChild[newText3];
document.body.appendChild[newParagraph3];

View the example in the browser

Example: [ - subtraction operator]

HTML Code:




JavaScript subtraction operator example 






JS Code:

var var1 = 45;
var var2 = 78;
var str1 = "w3resource";
var str2 = ".com";
var newvar = var1 - var2;
var newstr = str1 - str2;
var varstr = var1 - str2;
var newParagraph = document.createElement["p"];
var newText = document.createTextNode["var1 -var2 = "+ newvar];
newParagraph.appendChild[newText];
document.body.appendChild[newParagraph];
var newParagraph2 = document.createElement["p"];
var newText1 = document.createTextNode["str1 - str2  = "+ newstr];
newParagraph2.appendChild[newText1];
document.body.appendChild[newParagraph2];
var newParagraph2 = document.createElement["p"];
var newText2 = document.createTextNode["var1 - str2  = "+ varstr];
newParagraph2.appendChild[newText2];
document.body.appendChild[newParagraph2]

View the example in the browser

Example : [ * multiplication operator]

HTML Code:



JavaScript multiplication operator [*] example with DOM








JS Code:

var var1 = 45;
var var2 = 78;
var var3 = 45.10;
var var4 = 178.12;
var newvar  = var1 * var2;
var newvar1 = var3 * var4;

var newParagraph = document.createElement["p"];
var newText = document.createTextNode["var1 * var2 = "+ newvar];
newParagraph.appendChild[newText];
document.body.appendChild[newParagraph];

var newParagraph2 = document.createElement["p"];
var newText1 = document.createTextNode["var3 * var4  = "+ newvar1];
newParagraph2.appendChild newText1];
document.body.appendChild[newParagraph2];

View the example in the browser

Example: [ / division operator]

HTML Code:




JavaScript / operator example 








JS Code:

var var1 = 45;
var var2 = 78;
var var3 = 1;
var var4 = 2;
var newvar  = var1 / var2;
var newvar1 = var3 / var4;

var newParagraph = document.createElement["p"];
var newText = document.createTextNode ["var1 / var2 = "+ newvar];
newParagraph.appendChild[newText];
document.body.appendChild[newParagraph];

var newParagraph2 = document.createElement["p"];
var newText1 = document.createTextNode["var3 / var4  = "+ newvar1];
newParagraph2.appendChild[newText1];
document.body.appendChild[newParagraph2];

View the example in the browser

Practice the example online

See the Pen javascript-common-editor by w3resource [@w3resource] on CodePen.

Previous: Read from and write to HTML document
Next: JavaScript: Arithmetic Special Operators [%, ++, --, - ]

JavaScript: Tips of the Day

setTimeout method

const firstPromise = new Promise[[res, rej] => {
  setTimeout[res, 500, 'one'];
}];

const secondPromise = new Promise[[res, rej] => {
  setTimeout[res, 100, 'two'];
}];

Promise.race[[firstPromise, secondPromise]].then[res => console.log[res]];

When we pass multiple promises to the Promise.race method, it resolves/rejects the first promise that resolves/rejects. To the setTimeout method, we pass a timer: 500ms for the first promise [firstPromise], and 100ms for the second promise [secondPromise]. This means that the secondPromise resolves first with the value of 'two'. res now holds the value of 'two', which gets logged.

Ref: //bit.ly/323Y0P6

What is an arithmetic operator in JavaScript?

In JavaScript, arithmetic operators take numerical values [either literals or variables] as their operands and return a single numerical value. There are four standard arithmetic operators, addition [+], subtraction [-], multiplication [*], and division [/].

Which of the following is a an arithmetic operator?

These operators are + [addition], - [subtraction], * [multiplication], / [division], and % [modulo].

What are the 4 types of arithmetic operators?

Definition. The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.

What are the 4 types of JavaScript operators?

JavaScript includes operators that perform some operation on single or multiple operands [data value] and produce a result. JavaScript includes various categories of operators: Arithmetic operators, Comparison operators, Logical operators, Assignment operators, Conditional operators.

Chủ Đề