JavaScript Learning Basics

Source: Internet
Author: User

basic syntax
    Javascript syntax and java language similar, each statement with; End, statement block with {...} wrapped up. javascript
    comments divided into line comments ( // ) and block annotation () two kinds.
data type
    number 
      //javascript no distinction between integers and floating-point numbers, unified with number number type:

var num1 = 123; integer 123

var num2 = 0.456;// floating-point number 0.456

var num3 = 1.2345e3; scientific notation means 1.2345x1000, equivalent to 1234.5

var num4 = -99;// negative

var num5 = nan;//nan means not anumber, which is expressed in Nan when the result cannot be evaluated

var num6 = infinity;//Infinity is infinitely large, and when the value exceeds the maximum value that JavaScript can represent, it is represented as Infinity

Number can do arithmetic directly, and the rules and math are consistent:

var num1 = (1 + 2) * 5/2; equivalent to Mathematics (1 + 2) X 5 ÷ 2

var num2 = 2/0;//Infinity

var num3 = 0/0;//NaN

var num4 = 10.5% 3;//for remainder operation , =1.5

string
The string is in single quotation marks'or double quotation marks"any text that is enclosed, such as' abc ',"XYZ"and so on. Please note that"'or""itself is just a representation, not part of a string, so the string' abc 'onlya,b,Cit3a character.

Boolean value
//A Boolean value is exactly the same as that of a Boolean algebra, and a Boolean value is onlytrue,falsetwo kinds of values, eithertrue, or isfalse, you can directly usetrue,falserepresents a Boolean value, or it can be computed by a Boolean operation:

True This is a true value

False This is a false value

2 > 1; This is a true value

2 >= 3; This is a false value

The && operation is associated with an operation, and only all is true, and theresult of the&& operation is true:

True && true; this && statement evaluates to true

True && false; this && statement evaluates to false

False && true && false; this && statement evaluates to false

//|| An operation is or an operation, as long as one of the true , || The result of the operation is true :

False | | False this | | statement evaluates to false

true | | False this | | statement evaluates to true

False | | true | | False this | | statement evaluates to true

//! operation is a non-operation, it is a single-mesh operator, true become false , false become true :

! True The result is false

! False result is true

! (2 > 5); result is true

Boolean values are often used in conditional judgments, such as:

var age = 15;

if (age >= 18) {

Alert (' adult ');

} else {

Alert (' teenager ');

}

When we Compare number, we can get a Boolean value from the comparison operator:

2 > 5; False

5 >= 2; True

7 = = 7; True

in fact,JavaScript allows comparison of arbitrary data types:

false = = 0; True

false = = 0; False

pay special attention to equality operator = =. at design time, JavaScript has two comparison operators:

The first is = = comparison, it will automatically convert the data type to compare, many times, will get very strange results;

The second is the = = = comparison, which does not automatically convert the data type if the data type is inconsistent, returns falseif consistent, and then compares.

because of the design flaws of JavaScript, do not use = = comparison, always adhere to the use = = = comparison.

Another exception is that the special number of NaN is not equal to all other values, including its own:

Nan = = = Nan; False

the only way to Judge NaN is through the IsNaN () function:

IsNaN (NaN); True

Finally, pay attention to the equal comparison of floating-point numbers:

1/3 = = = (1-2/3); False

This is not a design flaw in JavaScript. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals. To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are less than a certain threshold:

Math.Abs (1/3-(1-2/3)) < 0.0000001; True

Nulland theundefined
Nullrepresents a"Empty"the value, it and0and an empty string"'different,0is a numeric value that"'represents a length of0the string, andNULLrepresents"Empty". In most cases, we should useNULL. undefineduseful only when judging whether a function parameter is passed or not.

Array

An array is a set of sequentially arranged collections, each of which is called an element. An array of JavaScript can include any data type, such as :

var arr = [1,2,3.14, ' Hello ', null,true];

another way to create an array is through the array () function :

var arr1 = new Array (1,2,3.14, ' Hello ', null,true);

However, for the sake of readability of the code, it is strongly recommended to use []directly.

the elements of an array can be accessed through an index. Note that the starting value of the index is 0:

arr[0];// returns an element with an index of 0 , which is 1

arr[5];// returns an element with an index of 5, which is true

arr[6];// Index is out of range, return undefined

Object

A JavaScript object is a set of unordered collections of key-value, for example:

var person = {

Name: ' Zero ',

Age: ' 20 ',

tags:[' JS ', ' web ', ' mobile '/// Last attribute cannot have ' , 'because there will be an error in IE6

};

the key of a JavaScript object is a string type, and the value can be any data type. To get the properties of an object, you can use an object variable . property names in a way that:

person.name;//' Zero '

person.age;//' 20 '

JavaScript Learning Basics

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.