Home >Web Front-end >Front-end Q&A >Is there any difference between =, == and === in javascript?

Is there any difference between =, == and === in javascript?

青灯夜游
青灯夜游Original
2021-10-19 12:01:164657browse

There is a difference. In JavaScript, "=" is an assignment operator, which assigns the right-hand value to the left-hand variable; while "==" and "===" are comparison operators, used to compare whether the expressions on the left and right sides are equal. . "==" will perform type conversion of necessary values ​​before equality comparison; while "===" will not perform type conversion.

Is there any difference between =, == and === in javascript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The difference between =, == and === in javascript

"=" is the assignment operator, "== " and "===" are comparison operators

  • "=" operator

    "=" is the simplest assignment operator, which will The value on the right side of the operator is assigned to the variable on the left side of the operator

  • The "==" and "===" operators

    are used for comparison operations Whether the expressions on the left and right sides of the symbol are equal.

"==" represents equality, "===" represents strict equality

When comparing double equal signs: First check the data types of the two operands. If they are the same, then perform === comparison. If they are different, we are willing to perform a type conversion for you. Convert them to the same type before comparison. During === comparison, if the types are different , directly false

  • Double equal sign==:

     (1) If the two value types are the same, then three equal signs Comparison of (===)

     (2) If the two value types are different, they may be equal. Type conversion needs to be performed according to the following rules before comparison:

     1) If one is null, One is undefined, then they are equal

      2) If one is a string and the other is a numerical value, convert the string into a numerical value and then compare

  • Three Equal sign ===:

     (1) If the types are different, they must not be equal

     (2) If both are numerical values ​​and they are the same value, then they are equal ; Not equal if at least one of them is NaN. (To determine whether a value is NaN, you can only use isNaN() to determine)

     (3) If both are strings and the characters at each position are the same, then they are equal, otherwise they are not equal.

     (4) If both values ​​are true or false, then they are equal

     (5) If both values ​​refer to the same object or function, then they are equal, otherwise they are not Equality

     (6) If both values ​​are null or undefined, then they are equal

[Recommended learning: javascript video tutorial]

The above is the detailed content of Is there any difference between =, == and === in javascript?. 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