JavaScript Basic article

Source: Internet
Author: User

1, JS has several data types?

JS consists of 5 simple data types: Undefined, Null, Boolean, number, String, and a complex data type: object.

2, the role of typeof?

If we want to know which data type a variable belongs to, then you can use the TypeOf operator for that value. The typeof return value has the following 6 types:

"Undefined": if this value is not defined. such as VAR ABC; Console.log (typeof ABC); Undefined

"Boolean": If this value is Ture or Fasle.

"String": If the value is a string.

"Number": if the value is numeric.

"Object": If the value is an object or null. NULL is essentially a null pointer object, so typeof null would return object.

"Function": if the value is a function. such as var a = function () {}; Console.log (typeof a);//function;

3. JavaScript's ternary operator?

Because the ternary operator is often encountered in the development process, and very useful, it is simple to describe the use of the next method. The specific expression is as follows:

Variable = boolean_expression? True_value:false_value;

The meaning of this code is to assign a value to the variable variable, which value depends on the state of Boolean_expression, and if Boolean_expression is true, True_value is assigned;

If Boolean_expression is false, Fu False_value;

such as var a = 3; var B = 4; We think that the larger value of a and B is given to C, so it can be written.

var c = a > B? A:B; Console.log (c);//4;

4. How do I create an object?

There are two main ways of creating an object:

(1), var p = new Object ();

You can then add attributes to the inside. Like what:

P.name = "Xiaoming";

P.age = 22;

(2) and the second is to create objects in a literal way.

var p = {

Name: "Xiaoming",

Age:22

};

The two ways of creating objects are the same, but the second syntax code is less and can give a sense of how the data is encapsulated, so the second method is basically used when creating objects.

5. How to create an array?

There are two ways to create an array.

(1) var array = new Array ("Red", "Blue", "green");

(2) var array = ["Red", "Blue", "green"];

6. How to create a function?

There are two ways to create a function.

(1), function declaration

function aaa (m) {

Console.log (m);

};

(2), function expression

var aaa = function (m) {

Console.log (m);

};

Difference: The biggest difference between the two is that the function created with the first method will have a function to declare the ascension process, what is the function declaration promotion, that is, the JavaScript engine will put the function declaration created by the function to the top of the source code tree, before the other code to execute.

For example: In the above two function expression preceded by a sentence AAA (5); The first one can be executed normally, the second will be an error.

                    

  

JavaScript Basic 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.