Data type conversions

Source: Internet
Author: User

1. Convert the string to a number:

The string can be multiplied by 1.

2. There is also a lazy way to convert other types to strings:

You only need to connect it to an empty string.

3. If a logical operation is performed on a non-Boolean value, the value is converted to a Boolean value during the calculation:

var B = "one";

!b;

False

4. Empty string

Null

Undefined

NaN

Number 0

Boolean value False

All of the above are false;

5. Precedence of the operator:

The multiplication takes precedence over addition.

logical operators! The highest priority. First && after | |

Best practice: Use parentheses as much as possible, rather than relying on operator precedence to set the order in which code executes, so that our code can be more readable.

6. Lazy Evaluation:

var B = 5;

true | | (b=6)

at this point output: true;

at this point the value of B is still 5;

Parse: Any one of these operands is true, and the result of the expression is true. Thus, when the first operand is evaluated, the result is determined, regardless of the subsequent value. So we can allow the JavaScript engine to steal a lazy. Omit some unnecessary evaluation operations without affecting the final result.

var B = 5;

(b=6) | | True

This time output: 6

At this point the value of B is 6;

Parsing: If the JavaScript engine encounters an operand of a non-boolean type in a logical expression, the value of the operand becomes the result returned by the expression.

7. Comparison operators

= = equality operator

Exp:1 = = ' 1 ' true

1 = = ' 2 ' false

1 = = 1 true

Returns True when two operands are equal. Operands on both sides are automatically converted to the same type before the operator executes

= = = equivalent operator

Exp:1 = = = ' 1 ' false

1 = = = 1 true

1 = = = ' 2 ' false

When and only if the values and types of the two operators return true simultaneously. This comparison is often more reliable because there is no open type conversion behind the scenes

! = inequality operator

Exp:1! = ' 1 ' false

1! = ' 2 ' true

Returns True when two operands are not equal (existence of type conversions)

!== not equivalent operator

Type conversion is not allowed within this operation, and returns True when two operation values or types are different

Exp:1!== 1 False

1!== ' 1 ' true

NaN is not equal to anything including itself. Nan = = Nan false

8.undefined and Null

When we try to access a variable that is undefined or unassigned, we get a undefined value. JavaScript automatically sets the declared but uninitialized variable to undefined;

And null is not the same, it can not be automatically assigned to variables by JavaScript, only through our code to complete.

Example: var somevar = null;

Somevar//null

typeof Somevar//object

Data type conversions

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.