Create the new and literal syntax of the string in Javascript

Source: Internet
Author: User

To declare a string in Javascript, we can easily use the literal (literal) Syntax:

VaR str1 = 'Hello JavaScript! ';
VaR str2 = "Hello world! ";

 

Since everything in Javascript is an object, we can also create it using the new syntax:

VaR str3 = new string ("Hello JavaScript! ");
VaR str4 = new string ("Hello world! ");

 

The question is, are these two methods equivalent to creating strings?

Str1 = str3 // true
Typeof (str1) = 'string' // true
Typeof (str3) = 'object' // true
Typeof (str3.tostring () = 'string' // true

 

It can be seen that the new string and the literal string use = to be equal, but they belong to different types. In JavaScript, you can use = to compare whether two variables are strictly equal (not only the literal value is equal, but also the type is equal ). Str1 = str3 // false

 

Note that when you use = for determination, JavaScript will automatically convert the type, and sometimes produce strange results: 12 = '12' // true
12 = '12' // false

 

This is why the jslint recommended ====instead of ===is used for variable judgment.
Related Article

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.