JavaScript Core Base syntax

Source: Internet
Author: User

A. How javascript is added to HTML:

1. External method:
The <script> content code </script> is written in a new JavaScript file and then introduced in the HTML file, which is added in the head:
<script type= "Text/javascript" src= "javascript file path" ></script>

2. Inline mode:
To include JavaScript script code in the HTML event handler properties
<body>
<input type= "button" value= "Press Me" onclick= "alert (' Hello ');" >
</body>

3. Embedded mode:
Add the "<script> Code </script>" section to the "head" \ "body" \ "html external" way.

Two. Original data type

1. typeof operator: Returns the type of data
typeof 1; Returns number;
typeof "ABC"; Returns a string
typeof true; Returns a Boolean
typeof null; Return object
typeof (undefined); Back to undefined

2. Undefined type: is an undefined type with only one value, that is, Undefined:
var a;(declared a variable, but not initialized)
Type A; The value returned is undefined
Type B; The value returned is undefined
The case is: The variable A is defined, but not initialized, when the default value of a is undefined;
The above does not declare the variable B whose type is Undefined, so the type also returns Undefined;

3. NULL type: Is NULL, indicates an object that does not exist, and has only one value, i.e. null;
alert (null==undefined); Output true
The representation of NULL is equal to undefined;
If the function or method returns an object, NULL is returned when the object is not found;


4. Boolean type: is a Boolean type with two values of true and False
var a = true; The declared variable A is a Boolean type

5. Number type: is a numeric type that can represent either 32-bit integers or 64-bit floating-point numbers
Several special values for the number type:
Number.MAX_VALUE Number Type Maximum
Number.min_value the minimum value of the number type
(all JavaScript numbers must be between these two values, but the calculated value can be not between these two numbers)

Number.POSITIVE_INFINITY = number of positive infinity of INFINITY
Number.nagetive_infinity =-infinity number of negative infinity
Isfinit (a) function: Determines whether the parameter a is a poor number, if it returns True, and returns false

NaN is a non-numeric representation, typically used for type conversions:
NaN is not equal to itself
IsNaN ("abc") function: Determine whether the string ABC can be converted to a number, if not, return true; reverse return false

6. String type: Is a string type, it is the only original type that does not have a fixed size
The string is declared with a single quote ', or double quotation mark, but use double quotation marks as much as possible.

Three. Some methods of string strings

String method (the original string of most methods does not change):

1. charCodeAt (Indext): var a = str.charcodeat (2); The original string does not change.

Returns an integer that is the ASCII value of the corresponding character of the returned parameter (index value).

2. Concat (): var ss = Str1.concat (STR2); The original string does not change.

The string str2 is stitched behind the string str1, returning the new array after stitching.

3. IndexOf (Str2,index): var a = Str1.indexof (str2,3); The original string does not change.

Finds a substring that returns the position of the first occurrence of a substring str2 in str1;

From STR1 3 index values start from left to right to find str2, if found, returns the index value of the first occurrence of str2;

If not found then return-1;

If index is negative, it is treated as 0;

If index is greater than the maximum index value of str1, it is treated as the maximum index value of str1;

If index is omitted, the representation is searched from left to right starting at 0.

4. LastindexOf (Str2,index): var a = Str1.lastindexof (str2,3); The original string does not change.

Finds a substring that returns the last position of the substring str2 in the str1;

From STR1 3 index values start from right to left to find str2, if found, return str2 the last occurrence of the index value;

If not found then return-1;

If index is negative, it is treated as 0;

If index is greater than the maximum index value of str1, it is treated as the maximum index value of str1;

If index is omitted, it represents the right-to-left lookup from the maximum index value.

5. Slice (Start,[end]): var a = Str1.slice (2,5); The original string does not change;

A string that returns a partial substring of the string str1, that is, the start--to--[end] index value;

Takes a string between start--and--end, but does not include the character of the end;

If start>end, no element is returned;

If start or end is negative, then the length value of start or end and str1 is added, the sum is calculated, and then as the index value (if the operation is start>end, there is no return value);

If End is omitted, all elements behind start are taken.

6. Substring (start,end): var a = str1.substring (2,5); The original string does not change;

A string that returns a partial substring of the string str1, that is, the start--to--[end] index value;

Takes a string between start--and--end, but does not include the character of the end;

If start or end is negative or Nan, then start or end is replaced with 0;

Four. Creation and application of arrays


There are several ways to create arrays:

1. The most common way: var array = [All-in-one, "a", "B", "C"];

2. var array = new Array (1,2,3,4,5);

3. var array = new Array ();
Array[0] = 1;
ARRAY[1] = 2;
ARRAY[2] = 3;

4. var array = new Array (3);
Array[0] = 1;
ARRAY[1] = 2;
ARRAY[2] = 3;

Array of methods:


1. Array.shift (): Delete the first element of the array, the array of arrays has changed

Deletes the first array element of an array of arrays;
Returns the element that was deleted;

2. Array.pop (): Delete the last element of the array, the original array has changed

Deletes the last array element of an array of arrays;
Returns the element that was deleted;

3. Array.unshift (parameter): Insert the data in front of the array, the original array changes;

The argument can be a number, a character, or another array;
Returns the length of the new array;


4. Array.push (parameter): At the end of the array to insert data, the original array has changed;

The argument can be a number, a character, or an array
Returns the length of the new array;


5. Array.splice (Index,count, "a", "B", "C"): Delete the elements in the array, the original array changes;

Returns the element that was deleted

Index: Indicates the start of deleting an element
Count: Indicates the number of deleted elements
A,b,c: Optional, if there is a,b,c, it means to insert the new data a,b,c the location of the deleted element;
If not, the location of the element is no longer inserted into the new data;


6. Array.reverse (): Reverse the array, return the new array, the original array changed;


7. Array.slice (Start,[end]): Returns some elements, the original array does not change

Returns the element between the start subscript-the--end subscript (excluding end-corresponding elements);

If start>end, there is no return element;
If start or end is negative, the system will convert the value to 0;
If start or end exceeds the maximum index value of the array, the system will convert the value to the maximum index value of the array;

Five. Data type conversions:

I. (String converted to a number) converts only the string before the first invalid character):

1. Convert to Integer: parseint ()

parseint ("123abc"); Returns 123

parseint ("22.5"); Returns 22

parseint ("abc"); Return Nan (denotes non-numeric)

2. Convert to floating point number: parsefloat ()

Parsefloat ("123abc"); Returns 123

Parsefloat ("22.5"); Returns 22.5

Parsefloat ("abc"); Return Nan (denotes non-numeric)

Parsefloat ("0908"); Returns 908

Ii. convert numbers to strings:

1. Declare the string variable directly: var a = "123"; String "123"

2. The most straightforward method: var str = 123 + ""; Converts the number 123 to characters

3. Use the ToString function:

var a = 10;

var B = a.tostring (); Returns the string "10"




Six. Force type conversions:

1. Number (value)-----Converts the given value to a digit (integer or floating point)

When the entire string is not fully converted to a number, NaN is returned, which is not the same as the partial conversion function parseint () and parsefloat ():

var a = number ("123"); A is given a value of 123

Number ("123abc"); Returns NAN (ABC has no number corresponding to it, so it returns Nan, representing non-numeric)

Number ("2.2.7"); Returns Nan (2.2.7 does not have this number, so it returns Nan, representing a non-number)

Number (false); Returns 0

Number (true); Returns 1

Number (NULL); Returns 0

Number (undefined); Return Nan

Number (new Object ()); Return Nan

2. Booleab (value)-----Converts a given value to a Boolean type

If the value of the conversion is: empty string "" or the number 0 or undefined|null----Boolean function will return false;

var a = Boolean (""); B is given a value of false

Boolean (0); Returns false

Boolean (NULL); Returns false

Boolean (undefined); Returns false

Boolean ("Hi"); Returns True

Boolean (123); Returns True

Boolean (New Object ()); Returns True

3. String (value)-----Converts the given value to a string (this function can convert any value to a string)

var S1 = String (null); Returns the string "null"

var S1 = String (undefined); Returns the string "undefined"

The difference between the String (value) and the ToString () function is:

If the ToString () function converts a value of null or undefined, an error occurs, and string () does not

Seven. Date object:

Create Date object: var now = new Date ();
The variable now is a Date object and the value is the time of day



Method of the date:
1. Now.getfullyear (); Get Current year
2. Now.getmonth () +1; Get Current Month
3. Now.getdate (); Gets the current day
4. Now.gethours (); Gets the current hour
5. Now.getminutes (); Gets the current minute
6. Now.getseconds (); Gets the current seconds

JavaScript Core Base syntax

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.