JavaScript Basics Getting Started tutorial (i)

Source: Internet
Author: User

Data type of JS

Although JS is a weakly typed language, it has a total of 6 data types. The weak type means that the defined variable has no type distinction, such as executing var num=123 first; Defines a variable num storage integer that can then be executed num= "Hello World"; Assigning a string to the NUM variable does not give an error and is legal.

The weak type of JS seems to be very useful relative to the strong type of language, in fact, not really, the following list of some strings and integers to operate when some of the problems caused by the weak JS.

// The result is // The result is "3232." // the result is 0.

/*
It is worth mentioning that based on some of the above questions, there are two types of conversion techniques
String to integer: num-0;
Integer to string: num + ';
*/
Six types of data in JS

Original type: ①number;②string;③boolean;④null;⑤undefined

Object type: ①object (function,array,date, etc. are all object types)

The implicit conversion of JS

Because JS is a weak type, there is a large number of implicit conversions, where the result is true

//try to convert the string to a number and then compare false //Convert the Boolean value to a number and then compare, where false turns to 0,true to 1,
So 1==true returns True, but 2==true returns falsenull = = undefinednewnew Object () [ [] = [+]
If it is Object==string|number will attempt to convert the object to a basic type such as: New string (' hi ') = = ' Hi ' will
Returns true, and other conditions return false.

So someone would ask, how does it make sure that 1.23 equals 1.23 and not equal to "1.23"? This leads to a new operator in JS = = strict equals operator, which first determines whether the type on both sides of the equal sign is the same, and returns false directly. If the type is the same and satisfies the principle, it returns true:

① Values Equal Value

② string satisfies both length and character equality

③null = = = NULL

④undefined = = = undefined

There are also two things Nan and object, where Nan and anything compare is false, including itself, that is, Nan===nan also returns false, and object is a reference comparison, and only the same object returns True.

JS Package Type

As shown, although the preceding string (not string) is a basic type, but it still has the property length, which seems to have an object shadow, which is actually the role of JS Wrapper, each time in the use of string, number and other types of variables, JS will automatically wrap these variables and create a temporary object, as shown in the figure, JS secretly executes the new String (str) when executing str.length. length.

JS Type detection

1. Using typeof detection Type

typeof100//returns "Number"typeof true    //returns "Boolean"typeof function    //returns "Function"typeof(undefined)//back to "undefined"typeof NewObject ()//returns "Object"typeof[Up]//returns "Object"typeofNaN//returns "Number"typeof NULL    //returns "Object"

2. Use instanceof to determine object type (format: Object instanceof function constructor)

instanceof // returns True New instanceof // returns false

3. Using Object.prototype.toString

Object.prototype.toString.apply ([]);    // returns "[Object Array]" Object.prototype.toString.apply (function() {});    // returns "[Object Function]" Object.prototype.toString.apply (null);    // returns "[Object Null]" Object.prototype.toString.apply (undefined);    // return "[Object Undefined]"

JavaScript Basics Getting Started tutorial (i)

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.