A detailed description of JavaScript arithmetic operators

Source: Internet
Author: User
Tags arithmetic operators

Arithmetic operators

+,-,*,/,%,+ +, --

+ +,-- divided into prefix form and suffix form

prefix form plus minus 1 in execution

The suffix form executes plus minus 1

Attention

+ number to connect two strings

as long as one of the + connected operands is a string type,JS automatically handles non-string data as String data.

the execution order of the JS code is left to right, so in the + connected expression, all occurrences of numeric data ( or can be automatically converted to numeric data) before the string data is encountered ) is still treated as a numeric value. To avoid this situation, we can spell an empty string before the expression

Instance

<! DOCTYPE html>

<meta charset= "Utf-8" >

<title></title>

<body>

<script type= "Text/javascript" >

examples of arithmetic operators

var i=3,j=8;

document.write (I+J);

document.write (' <br/> ');

document.write (I-J);

document.write (' <br/> ');

document.write (I*J);

document.write (' <br/> ');

document.write (i/j);

document.write (' <br/> ');

document.write (I%J);

document.write (' <br/> ');

document.write (3%-8);

document.write (' <br/> ');

document.write ( -3%8);

document.write (' <br/> ');

document.write ( -3%-8);

document.write (' <br/> ');

+ More Special

document.write (i+ ' 8 ');

document.write (' <br/> ');

document.write (' 2 ' + ' 8 ');

document.write (' <br/> ');

document.write (i+j+ ' 3king ');

document.write (' <br/> ');

document.write (' +i ');

var z= ' +i;

Alert (typeof z);

document.write (' <br/> ');

document.write (' +i+j+ ' 3king ');

document.write (' <br/> ');

self-increment decrement operator + +--

integer Support self-increment decrement operator

var num1=1;

alert (num1++);

alert (NUM1);

alert (++NUM1);

alert (--NUM1);

floating-point support self-increment decrement operator

num1=12.3;

alert (--NUM1);

Num1=true;

Num1=false;

alert (++NUM1);

alert (--NUM1);

The self-increment decrement operator is not supported for string types

num1= ' 3b ';

alert (++NUM1);

alert (--NUM1);

Null support for self-increment decrement operator

Num1=null;

alert (++NUM1);

alert (--NUM1);

num1=undefined;

alert (++NUM1);

</Script>

</body>

Operation Result:

original link:http://www.maiziedu.com/wiki/js/sum/

A detailed description of JavaScript arithmetic operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.