Home > Web Front-end > JS Tutorial > body text

Detailed analysis of operators i++ and ++i in JS

jacklove
Release: 2018-06-15 15:03:50
Original
2817 people have browsed it

Please look at the following code:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <script>
        var a=10,b=20,c=30;
        ++a;
        a++;
        e=++a+(++b)+(c++)+a++;
        alert(e);
    </script>
</body>
</html>
Copy after login

To answer this question, first understand: i What is returned is The value before auto-increment, i returns the value after auto-increment.

如:
var i = 1;
var a = i++;  //a = 1;  此时i先将值1赋给a,然后自己+1,i=2;
var b = ++i;  //b = 3;此时i先自己+1为3.再给b赋值,b=3;
Copy after login
再看此题:那么++a意思为a=a+1 a为11,此时不涉及运算优先级的问题。
Copy after login
		a++意思为a=a+1       a为12  ,此时仍旧不涉及运算优先级的问题。
Copy after login
		当e=++a+(++b)+(c++)+a++;时,必须考虑运算级的问题。
Copy after login

##

		此时:++a a先自己+1  然后将值赋给结果:13      a=13
Copy after login
			++b  b先自己+1  然后将值赋给结果:21      b=21
Copy after login
			C++ 先将c值赋给结果:30     c自己+1      c=31
Copy after login
			a++ 先将a值13赋给结果:13   a自己+1       a=14
			此时所有结果加起来:13+21+30+13=77
			本文讲解了JS中运算符i++与++i的详细分析,更多相关内容请关注php中文网。
			相关推荐:
Copy after login

The above is the detailed content of Detailed analysis of operators i++ and ++i in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!