Home >Web Front-end >JS Tutorial >Detailed analysis of operators i++ and ++i in JS

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

jacklove
jackloveOriginal
2018-06-15 15:03:502836browse

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>

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;
再看此题:那么++a意思为a=a+1 a为11,此时不涉及运算优先级的问题。
		a++意思为a=a+1       a为12  ,此时仍旧不涉及运算优先级的问题。
		当e=++a+(++b)+(c++)+a++;时,必须考虑运算级的问题。

##

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

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!

Statement:
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