Straighten out JavaScript (4)-variables, constants, and Data Types

Source: Internet
Author: User
VaR V1 = 123;/* declare the variable and initialize it as a number */var v2 = 'abc';/* declare the variable and initialize it as a string */var V3, V4; /* declared variables that have not been initialized, type unknown (unknown is also a type: Undefined) */x = 1; y = 2;/* Missing VAR (not declared) the variable can also be used, but it will make the compiler supplement the Declaration behind the scenes; it is best not to do this */Alert (x + y);/* 3 */
 

How many data types does JavaScript have? It is better to parse from the example:

VaR X;/* declare a variable X */Alert (typeof X);/* use the typeof function to check whether its type is undefined */x = 123; /* assign the numeric value */Alert (typeof X);/* The type is Number */x = 'abc '; /* assign the string value */Alert (typeof X);/* The type is string */x = true;/* assign the Boolean value (true or false) */Alert (typeof X);/* type: Boolean */x = function () {};/* assign a function */Alert (typeof X ); /* type: function */x = new array (, 3);/* assign an array */Alert (typeof X);/* type: object */x = new date ();/* assign a date value */Alert (typeof X);/* The type is: object */x = new Regexp ();/* assign a regular expression object */Alert (typeof X);/* The type is: object */x = new string ();/* assign a String object */Alert (typeof X);/* The type is: object */x = new Boolean ();/* assign a Boolean object */Alert (typeof X);/* The type is: object */x = new number ();/* assign a numeric object */Alert (typeof X);/* The type is: object */x = new error ();/* assign an error object */Alert (typeof X);/* The type is: object */x = new object ();/* assign an object */Alert (typeof X);/* The object type is: object */
 

Summarize the Javascript data types from the example:

Undefined/* type of the variable without a value or does not exist */number/* numeric type */string/* string type */Boolean/* boolean type */function/* function type * /Object/* object type, arrays and so on all belong to the object type */
 

You can verify from the practice:

Alert (typeof window);/* object */Alert (typeof window. screen);/* object */Alert (typeof window. closed);/* Boolean */Alert (typeof alert Doc ument);/* object */Alert (typeof alert Doc ument. body);/* object */Alert (typeof entity Doc ument. body. offsetwidth);/* Number */Alert (typeof alert Doc ument. body. clientheight);/* Number */Alert (typeof temporary upload Doc ument. title);/* string */Alert (typeof temporary upload Doc ument. title);/* undefined * // * The Last One above is undefined, which indicates that the object or attribute does not exist. Because Javascript is case sensitive, the document object does not contain the title object or attribute, the document title attribute should be title */
 

We can see from the front:
There is both a string type and a string object;
There is both a number type and a number object;
There is both a boolean type and a bollean object...

This is a hassle caused by Js for simplicity. In fact, you don't need to consider so much. You can mix string and string to use it. The JS compiler will handle everything behind the scenes. For example:

VaR x = new string ('abc');/* X is a string object */var y = new string ('20140901 '); /* Y is a string object */var xy = x + y;/* xy is a string type */Alert (xy);/* ABC123 */Alert (XY. length);/* 6 */var x = 'abc';/* in this case, X is of the string type */var y = '123 '; /* at this time, Y is of the string type */var xy = new string (x + y);/* xy is a string object */Alert (xy ); /* ABC123 */Alert (XY. length);/* 6 * // * The above String Length attribute (length) is originally a String object, and the string type string is still used; this may be because the compiler quickly performs a conversion. In JS, any type of conversion is convenient and free. */
 

When it comes to the simplification of the Javascript type, its value type has only one number (similar to the double type of Delphi );
There is no single character type, and a string with a length of 1 is required.

Similar to the Delphi pointer, you can assign a null value to the JS variable: NULL; This NULL is not the same as the undefined above.
Null is a value, but null;
Undefined is not assigned a value or does not exist.

Alert (undefined = NULL);/* true; simply put, they are almost */Alert (undefined = NULL);/* false; read carefully, they are different * // * null as null values (equivalent to 0) can be involved in numerical operations */var X; X = 123 + NULL; alert (X ); /* 123 */x = 123 + undefined; alert (x);/* Nan * // the idea of JS should be like this: // The unknown type is also type, if no value is assigned, it is also a value: undefined; // The null value is also a value: null.
 

Here is an example of null and undefined:

There is also a "constant" problem.
JS has no constants. The so-called constants are just variables that are assigned values in advance.
Infinity;/* infinity */nan;/* Non-numeric value */number. max_value;/* maximum value (which can be expressed by JS) */number. min_value;/* Minimum value (which can be expressed by JS) */number. nan;/* Non-numeric value */number. negative_infinity;/* negative infinity */number. positive_infinity;/* infinity * // * value test: */Alert (number. max_value);/* 1.7976931348623157e + 308 */Alert (number. min_value);/* 5e-324 */Alert (number. NAN);/* Nan */Alert (number. negative_infinity);/*-infinity */Alert (number. positive_infinity);/* infinity */Alert (infinity);/* infinity */Alert (NAN);/* Nan */* constants, such as (IE7 ): */Alert (window. navigator. appcodename);/* mozilla-browser Code */Alert (window. navigator. appname);/* Microsoft Internet Explorer-browser name */Alert (window. navigator. appversion);/* 4.0... -browser version */
 

In addition, the global variables of js in the browser are directly related to the window object of the browser, although this prefix is often omitted:

var X = 111;alert(window.X); /* 111 */window.Y = 222;  alert(Y);        /* 222 */function MyFun(x, y) {  return x + y;}alert(MyFun(1, 2));        /* 3 */alert(window.MyFun(1, 2)); /* 3 */
 

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.