JavaScript for the front-end base

Source: Internet
Author: User
Tags abs script tag

First, the history of JavaScript

Slightly


Second, ECMAScript

Note: ES6 means ECMAScript 6.

Although ECMAScript is an important standard, it is not the only part of JavaScript, and certainly not the only one that is standardized. In fact, a complete JavaScript implementation is made up of the following 3 different parts:

Core (ECMAScript)

Document Object Model (DOM) Documents object models (consolidated js,css,html)

Browser object models (BOM) Broswer object Model (integrated JS and browser)

Simply put, ECMAScript describes the content of the JavaScript language itself.

JavaScript is a scripting language

JavaScript is a lightweight programming language.

JavaScript is a programmatic code that can be inserted into an HTML page.

When JavaScript is inserted into an HTML page, it can be performed by all modern browsers.


Third, JavaScript introduction method

1. Write code inside the script tag of the HTML file

<script>

Write your JS code here

</script>

2, HTML file to introduce additional JS file

<script src= "Myscript.js" ></script>


Iv. JavaScript Language Specification

1, Comments (note is the mother of code)

This is a single-line comment

/*

This is a multi-line comment

*/

2, Terminator

The statements in JavaScript are terminated with a semicolon (;).


V. JavaScript language Basics

1. Variable declaration

The variable name of JavaScript can be composed of _, number, letter, $, and cannot begin with a number.

declaring variables using var variable names; The format to declare

var name = "Alex";

var age = 18;

Attention:

Variable names are case-sensitive.

Camel-named rules are recommended.

Reserved words cannot be used as variable names.


Vi. JavaScript data types

1. JavaScript has a dynamic type

var x; At this point x is undefined

var x = 1; At this point x is the number

var x = "Alex"//At this time X is a string

2. Number Type

JavaScript does not differentiate between integral and floating-point types, there is only one numeric type.

var a = 12.34;

var B = 20;

var c = 123e5; 12300000

var d = 123e-5; 0.00123

3. There is also a Nan, which indicates that it is not a digit (not a number).

Common methods:

parseint ("123")//Return 123

parseint ("ABC")//Return Nan,nan property is a special value that represents a non-numeric value. This property is used to indicate that a value is not a number.

Parsefloat ("123.456")//Return 123.456

4. String

var a = "Hello"

var B = "world;

var C = a + B;

Console.log (c); Get HelloWorld

Common methods:

Method Description
. length return length
. Trim () Remove whitespace
. Trimleft () Remove the left margin
. TrimRight () Remove the blank on the right
. CharAt (N) Returns the nth character
. concat (value, ...) Stitching
. indexOf (substring, start) Sub-sequence position
. substring (from, to) Get sub-sequences by index
. Slice (start, end) Slice
. toLowerCase () Lowercase
. toUpperCase () Capital
. Split (delimiter, limit) Segmentation


Splicing strings generally use "+"

Supplement: The difference between slice and ubstirng:

String.slice (Start, stop) and string.substring (Start, stop):

The same point of the two:

Returns an empty string if start equals end

If the stop argument is omitted, it is taken to the end of the string

If a parameter exceeds the length of a string, this parameter is replaced by the length of the string

Features of Substirng ():

If Start > Stop, start and stop will be swapped

If the argument is negative or not a number, it will be replaced by 0

Features of Silce ():

If Start > Stop does not exchange both

If start is less than 0, the cut starts with the first character of the ABS (start) from the end of the string, including the character at that position.

If stop is less than 0, the cut ends at the end of the ABS (stop) character of the forward number from the end of the string (does not contain the position character)

5, the Boolean type is different from python,true and false are lowercase.

var a = true;

var B = false;

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

6, the array is similar to the list in Python.

var a = [123, "ABC"];

Console.log (a[1]); Output "ABC"

Common methods:

Method Description
. length The size of the array
. push (Ele) Trailing append Element
. Pop () Gets the trailing element
. Unshift (Ele) Head Insert Element
. Shift () Removing elements from the head
. Slice (start, end) Slice
. Reverse () Reverse
. Join (SEQ) Concatenate array elements into a string
. concat (Val, ...) Connection array
. Sort () Sort

Add: Questions about the sort:

If no parameters are passed when the sort method is called, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded. To do this, you should first convert the elements of the array to a string, if necessary, for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:

If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.

If a equals B, 0 is returned.

If a is greater than B, a value greater than 0 is returned.

Implement a sort function yourself according to the rules above:

function Sortnumber (A, b) {

Return A-B

}

When you call the Sort method, the defined sort function is passed in.

Stringobj.sort (Sortnumber)

The elements in the array can be traversed in the following ways:

var a = [10, 20, 30, 40];

for (Var i=0;i<a.length;i++) {

Console.log (i);

}

7, null and undefined

Null indicates that the value is empty and is generally used when a variable needs to be specified or emptied, such as name=null;

Undefined indicates that when a variable is declared but not initialized, the default value of the variable is undefined. There is also the undefined that is returned when the function has no definite return value.

Null means that the value of the variable is empty, and undefined means that only the variable is declared, but it has not been assigned a value.

8. Type Inquiry

typeof is a unary operator (like ++,--,! ,-the unary operator), is not a function, nor is it a statement.

typeof "ABC"//"string"

typeof null//"Object"

typeof True//"Boolean"

typeof 123//"number"

Calling the TypeOf operator on a variable or value returns one of the following values:

Undefined-If the variable is of type undefined

Boolean-If the variable is of type Boolean

Number-If the variable is of type number

String-If the variable is of type string

Object-If the variable is a reference type or a Null type


JavaScript for the front-end base

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.