Values and Types

Source: Internet
Author: User

Type

The variables in JS are of no type, only the value has the type, and the variable can accept any type at any time. For example:

var a =;        Console.log (typeof//number        a = ' + ';        Console.log (typeof a); // string

Undefind and Undeclared

In JS, a variable that has been declared in scope but not yet assigned a value is undefined. The variable that is not yet declared in the scope is undeclared

var A;console.log (typeof//Console.log (b); // uncaught referenceerror:b is not defined

It is important to note that if you use typeof undeclared, you will return to undefined.

Console.log (typeof B); // undefined

However, one advantage of this approach is that typeof can be used to check if a variable is declared without an error. For example:

if (b) {   // This method will error    //do something}if (typeof B !== "undefined") {    //Dosomething}if// This is also possible, but if the code is executed in another environment, there are problems, such as Nodejs, where the global object is not window.     //dosomething}

Array

You can put any type of element inside an array, or you can create a sparse array.

var a = [];a[0] = ' 1a '; a[2] = ' 23 ';

It is important to note that the value of a[1] is undefined.
An array can also have its own properties like an object, such as:

a[' name '] = ' Yu '; Console.log (a.length); // 3

However, the property of the array does not change the length property. It is also important to note that if the property name can be converted to a decimal number, then it will be treated as an index. For example:

a[' + ') = ' a '//
Class Array

Generally in some DOM query operations return the list of elements, in fact they are not the real meaning of the array, but some class array. An array of classes is often used inside JQ.

var a = {1: ' A ', 2: ' B ', length:2};

Another example is that arguments is an array of classes, but at the beginning of es6 it has been abolished.
The usual way to convert an array of classes into real arrays is:

var arr = Array.prototype.splice.call (a);

In Es6, a new built-in function array.from () can also be used to transform functions, such as:

var arr = Array.from (a);

String

Strings and string arrays look the same in many cases, but they are not the same thing. They all have the length attribute and the IndexOf method.

var a = ' abc '; var b = [' A ', ' B ', ' C '];console.log (a.length); // 3Console.log (b.length); // 3console.log (a.indexof (' B ')); // 1console.log (b.indexof (' B ')); // 1

But the string is immutable, only a new string is created and returned, and the string array can be changed at the original value.

A[1] = ' V '; b[1] = ' V ';
Console.log (a); // ABCconsole.log (b); // ["A", "V", "C"]

In the actual development process, we can borrow the array method to achieve some purposes.

var c = Array.prototype.join.call (A, '-'); Console.log (c); // a-b-c

There is also a common face question that is the string reversal problem.
There is an reverse method on the array that reverses the array elements, but we cannot borrow them here because the strings are immutable.
But we can first turn the string into a group, in reverse.

var a = ' xyz '; var b = A.split ('//Z, y, x

Digital

Values and Types

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.