JavaScript on the front

Source: Internet
Author: User

I. How JavaScript is introduced

1. In the script tag: <script> JS code </script>

2. Introduction of external JS file: <script src= "JS filename or location" ></script>

Two. JavaScript Language specification

1. Note: ①.//single-line comment

②./* Multiline Comment */

2. Terminator: The statements in JavaScript are separated by semicolons (;). As Terminator

3. Variable name: JavaScript variable names can be used _, numbers, letters, $ composition, can not start with a number.

※: Variable names are case-sensitive, camel-style rules are recommended, reserved words (keywords) cannot be used as variable names

4. Declaring variables: Using the var variable name method

Three. JavaScript data type

  1. JavaScript has a dynamic type:

①. var x; typeof (X) is undefined

②. var x=1; typeof (X) is number

③. var x = "haha"//typeof (X) is string

  2. JavaScript numeric types do not differentiate between integral and floating-point type:

①. var a = 12.5; typeof (a) is number

②. var B = 20; typeof (b) is number

    ※. Nan means not a numeric value (not a number), but Nan itself is a numeric type

③. Common methods:

I:parseint ("123")///return 123 meaning: Parse string returns an integer value

Ii:parseint ("125.78")//Return 125

Iii:parseint ("ASD")//Return Nan

Iiii:parsefloat ("123.45")//Return 123.45

  3. Strings (String)

var a = "Hello";

var B = "World";

var C = a + B; string concatenation generally uses "+"

Console.log (c); Results: HelloWorld

string method Description
. Length return string length
. Trim () Remove both ends of a string
. Trimleft () remove spaces to the left of the string
. TrimRight () remove spaces to the right of the string
. CharAt (n) returns the character with index N (space count character)
. Concat ("String 1", "String 2",.....) to stitch together the parameters of the original string and the Concat
. IndexOf ("To find sequence", starting index position) Find the position of a substring in a string
. Substring ("Start index position", "End index position") get sub-sequences within a string based on index
. Slice () slice
. toLowerCase () turns the letter lowercase
. toUpperCase () to capitalize letters
. Split () cut (same as Python usage)

  4. Boolean Value (Boolean)

Unlike Python, both true and false are lowercase

var a = true;

var B = false;

    "" (empty string), 0, null, undefined, Nan, all Boolean values are false.

  5. Null and undefined

①. NULL means null, which is typically used when a variable needs to be developed or emptied.

②. Undefined indicates that when a variable is declared but not assigned a value, the default value of the variable is undefined. When the function does not have an explicit return value, the return is also undefined.

  6. Arrays

The function of an array is to store a series of values using a separate variable name. Similar to the list in Python

var a = [123, "ABC"];

Console.log (a[1]); Back to ABC

common methods :  

Array method description
. Length array size (length)
. Push ( "character") appends an element to the end of the array, returning the size (length) of the array
. Poo () Deletes an element (by default at the end of the delete), returning the deleted element
. Unshift ("character") inserts an element at the beginning of the array, returning the size (length) of the array
. Shift () removes the element from the header, returning the deleted meta
. Slice () slice (similar to Python usage)
. Reverse () reverses the array, in the original array base CK on table As
. Join ("join symbol") concatenate array elements into strings through connection symbols
. Concat ("character 1/array 1", "character 2/array 2", ....) add character/array to array (default to end)
. Sort () sorting (based on ASCII ordering of elements)
. ForEach () passes each element of an array to a callback function
. Splice (a,b,c) removes all elements indexed as A~B and adds C characters to the A~b index position
. Map () returns an array element that is worth a new array after the function is called

Four. Operators

1. Arithmetic operators

+-*/% + + (+ = in Python)--(-= in Python)

2. Comparison operators

> >= < <= = = = = = = =!==

※. = = Only determine if the values are equal 1 = = "1"//true

※. = = = determine value and data type 1 = = = "1"//false

3. Logical operators

&& | | !

4. Assignment operators

=  +=  -=  *=  /=

Five. Process Control

  1. If-else

var a = 10;

if (a > 5) {

Console.log ("yes");

}else{

Console.log ("no");

}

  2. If-else If-else

var a = 10;

if (a>5) {

Console.log ("a>5");

}else if (a<5) {

Console.log ("A<5")

}else{

Console.log ("a=5")

}

  3. Switch

var a = 5;

Switch (a) {

Case 0;

Console.log ("a = 0");

Case 1;

Console.log ("a = 1");

Break

Case 2;

Console.log ("a = 2");

Break

Case 3;

Console.log ("a = 3");

Break

Case 4;

Console.log ("a = 4");

Break

Case 5;

Console.log ("a = 5");

Break

}

    ※. The case clause in switch usually adds a break statement, otherwise the program executes the child statements of the subsequent case.

4. For

for (Var i=0;i<10;i++) {

Console.log (i);

}

Parameter description: initial value of I=0:I

I<10: Limits the range of I

i++: i=i+1

5. While

var i=0;

while (I<10) {

Console.log (i);

i++;

}

6. Ternary operation

var a = 1;

var B = 2;

var c = a > B? A:b

Description: Determine if A is greater than B, if a > B is established, C = A. If a > B is not valid, C = b

JavaScript at the front

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.