JavaScript language Core

Source: Internet
Author: User
Tags null null

ECMAScript: is a set of standard, core, definition specification, syntax, data type, basic object, keyword ...

HTML tag type: block, inline, inline-block, table ... Type allows us to distinguish between different types of tags knowing what style to define it. For example, the block type can be set to a wide height.

Data types in javascript:

/*

Classify data types according to typeof:

Number (NaN), String, function, Boolean, object (obj, element in document, [], {}, NULL), undefined (1 really does not define 2 Although the definition does not give a value)

The standard advocates two types of data:

Base type: number, String, function, Boolean, null, undefined

Type: Object

*/

1 var i = 100; 2 alert (typeof i); Number 3  4 var s = ' abc '; 5 alert (typeof s);//string 6  7 var B = true; 8 alert (typeof b);//boolean 9//if (12 <90) If the values in the parentheses are automatically converted to Boolean type Boolean values only ture, false10 one function fn1 () {alert (1);} Alert (typeof fn1); function13 var obj = window;15 var obj = document;16 alert (typeof obj); object17 var arr = [1,2,3,4];19 alert (typeof arr);   object20 var json = {'     name ': ' Xiaowang ', ' Age     ': 524};25 alert (typeof JSON);  object26 var n = null;28 alert (typeof N);  object29 var u;31 alert (typeof u);   Undefined

In addition to object types other than empty objects, you can add custom properties.

Data type conversions:

HTML tag types can also be converted, such as inline conversion to block.

var a = ' 100 ';

alert (a+100); 100100 String Connection

The string is converted into a numeric type:

Force type conversion/Display type conversion number (); parseint (); Parsefloat ();

1) Method:

Number ();

var a = ' 100 '; ' 000100 ', ' +100 '

Alert (number (a)); 100

var a = ';

Alert (number (a)); 0

var a = true;

Alert (number (a)); True 1 False 0

var a =function () {alert (1);};  Alert (number (a)); NaN can't go.

var a = []; 0

var a = ["]; 0

var a = [123]; 123

var a = [' 123 '];//123

var a = [n/a]; NaN

var json = {}//nan not go

var a = null//0

var A; Alert (number (a););//nan

Summary: JSON, undefined, function, and multi-object arrays are all Nan and cannot be transferred.

parseint (a,10); Parsefloat (a,10); By default there are two parameters, and the rear represents 10 binary

var a = ' 100px ';

The number () method is Nan, because the method is to convert the whole;

parseint (a)//100 This method is one by one, if the discovery is no longer a number is truncated. This method can only turn 200px to extract the numbers, and the other true functions are Nan. It's usually used to turn the string that is the number in front

' +200 ' Both of these methods can be turned, +-space, etc. are recognized. It will continue to run back to see if there are any numbers extracted.

var a = ' 123,400 yuan '; parseint//don't know the decimal point parsefloat//only know the first decimal point 12.34

Implicitly-Typed conversions:

+ 200+ ' 3 '//2003 turns into a string

-*/% ' 200 '-3//197 turn into digital

+ +--var a = ' ten '; A++;alert (a); 11 turn into a number

>< alert (' >9 '); True turns into a number

Take the counter alert (! ' OK ');//false no matter! What's on the right is going to turn into a Boolean value

= = Alert (' 2 ' ==2); True no matter what the data type is

Alert (' Ten ' > ' 9 '); False the comparison of the string and the comparison of the number is different, will be a one of the comparison 1:9 corresponding code value is small, so false. No longer compare to the future

= = = Alert (' 2 ' ===2); Two different data types with congruent, first judge the type and then judge the value, more rigorous than = =, but there is no data type conversion

Nan Applications:

1,var a = number (' abc '); alert (a);//nan alert (typeof a); Number so the numbers and number types are different. Nan is a numeric type but not a number

2, once the program appears Nan, indicating that the illegal operation must be done

3,nan is Fasle if (NaN)

4, numbers and strings compared to themselves are true,nan compared to their own is false

NaN is not a numeric type of number

IsNaN () is not a number (not a number)

IsNaN (); True false//To determine if certain values are numbers; Don't like numbers, hate numbers;

Alert (IsNaN (' 250 ')); False the internal first will be given to number () conversion and then judge the

Alert (IsNaN (true)); False number converted to 1

It doesn't hate number types (such as Nan) hate numbers

Alert (IsNaN (NaN)); True

Operator:

1) Arithmetic:

+ Converts a number to a string in a type conversion

-*/% can convert a string to a number in a type conversion, if the conversion is not a Nan

2) Assignment: =, + =,-=, *=,/=,%=

3) Relationship:<, >, <=, >=, = =,! = (unequal), = = =,!== (all ranges)

4) Logic:&& and, | | OR,! No

var a = 20<90&&20;alert (a); Pop-up 20, left set will go to the right

var a = 120<90&&20;alert (a); Eject false

var a = 120<90| | 20;alert (a); Eject 20, even if the left is false, will also go to the right

Program Flow control:

1) Judgment:

if () {}else if () {}else{} This form can also be written with switch

The If () {}else{} form can be written using the three-mesh (ternary) operator

2) Loop: while, for

3) Jump out: Break jump out of the loop, continue skip this cycle

To pass the parameter:

When you do something, if you find that two or more places on the page are almost the same, you can use the function to pass the parameter, this time should have a sense of communication, such as large portals on the home page many tabs, the same page has several sets of picture switch. To implement code reuse, take something different and pass it through the parameters. (such as ID, event, etc. can be used as parameters). When writing, try to ensure the following points:

1) Try to ensure that the same HTML code can be selected by the parent sub-elements;

2) The core of the main program to implement first, and then use a function package up;

3) to find out the different values of each group, through the transfer of parameters to achieve.

Summary of the last true and false questions:

True: True, numbers that are not 0, non-empty (spaces are also non-empty) strings, non-empty objects (functions, elements that can be found, [], {})

False: False, 0, empty string ("'), null null object, undefined (element not found)

JavaScript language Core

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.