JS Getting Started (iii)

Source: Internet
Author: User

type conversion of data :

As mentioned before, the data types in JS are divided into two kinds,

Basic data Type string number Boolean undefined null

Complex data type Object Date Array function

And in the actual programming, often with to the data type conversion problem, such as the string type of data into a number type, or the conversion of the number type into a Boolean type, that JS in the end how to implement the data type conversion?

turn into number :

Implicit conversions +-*/%; it is worth noting that + is used here as a positive and negative action, and is placed in the worth of the conversion of the front.

Casting a Data transformation using an external method or function

Number () parseint () parsefloat ()

var a = "123";
var B = true;
Console.log (number (a)); 123
Console.log (number (true)); 1
Console.log (number (null)); 0
Console.log (number (undefined)); NaN
Console.log (Number ("123.456")); 123.456
Console.log (Number ("")); 0

The value of the Boolean type is converted to number type true-->1 false-->0;

unerfined-----> NaN;

If the value can be converted to a number, it is converted to the corresponding value, and if it cannot be converted, it is called Nan.

The number () function features:

A. If the converted content can be turned into a number, then the number corresponding to this content is returned directly.

B. Convert the whole, and return nan if there is one that cannot be converted.

C. If a decimal is present in the content, the decimal number is preserved and the decimal is also output.

D. If the content is empty (null), then convert to 0;

turn into a string Type:

Implicit type conversion: + ""

Force type toString () String ();

Everything is object, JS object almost all have ToString (); null,undefined is not the ToString () method, if you want to cast, you can only use String (), the object can be converted with ToString (), or string ().

Go to Boolean type

  Implicit type! !

Coercion Type Boolean ()

  When the data type is turned to boolean , the case is false :

  0, "", Null,undefined,nan, these turn into a boolean type when all are false, the others are true;

function:

Function: It is actually a piece of code, a section of code that can be executed at the time of use and can be reused. It extracts things that can be reused into functions, and treats things that are not reusable as arguments into functions.

Definition of the function:

function function name ( parameter ) {

function Body ( both executable or code to execute );

}

Functions have three elements: function names ( functions ), parameters, return values.

First of all, the function name, because JS is very sensitive to the case, so the name of the function and call the function when the case must be always, otherwise you can not call the function. Then is the parameter, that is, we provide to the function of the data, the function can be any number of parameters, or do not write parameters, if there are two or more parameters, the parameters and parameters with a half-width of the comma separated. Finally, it is the return value of the function. Each function has a return value, and if there is no explicit return value in the function, then the function returns underfined.

Function calls are usually called with parentheses in the function name. The name of the function ();

In the function, there is also the difference between the tangible parameter and the argument, the parameter is in the function definition, the parameters passed in, mainly in order to occupy the role of position. The argument is the value of the parameter passed in when the function is called. After the function is defined, when the function is called, the arguments of the function are copied into the parameters of the function definition, which corresponds to the previous one by one, and if the arguments are not the same as the formal parameters, they are preceded by the previous allocation, that is, the first few in the matching parameters. So the number of arguments and parameters can be different.

Some of the things that have to be mentioned about functions are the problem of his scope. The top-level scope of the JS programming environment is the scope under the Window object, called the global scope, and the variables in the global scope are called global variables. the variables inside the JS function cannot be accessed outside the function, but variables outside the function can be accessed within the function, and variables within the function are called local variables.

JS Getting Started (iii)

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.