JavaScript functions and JS package features at a glance

Source: Internet
Author: User
Tags mathematical functions rounds square root

Function four elements: return type, function name, argument list, function body

function type

Simple functions

1 function Ceshi () {2    alert ("This is the test"); 3 }4 Ceshi ();

Parametric functions

1 function Ceshi (A, b) {2    alert (a+b); 3 }4 Ceshi (2,3);

There is a default function (JS is not supported)

1 function Ceshi (a,b=5) {2    alert (a+b); 3 }4 Ceshi (2);

Functions that have return values

1 function Ceshi (A, b) {2    return a+b; 3 }4var c=ceshi (2,3); 5 alert (c);

Knowledge Points:

Naming conventions for variables: captions begin, not Chuxian special symbols, usually lowercase

Naming conventions for functions: first-Letter Lowercase hump method, Chifanle

Encapsulates a function function: Encapsulates a bubbling sort code

1 function Paixu (arr) {2   //Bubble Sort Code3   var arr=new Array (45,56,23,78,89,99,36,77,100);4   var zj=0; Intermediate Variables5for (i=1;i<arr. length;i++) {//Used to control rounds6 For (J=0;j<arr.length-i;j++) {//To control the number of times7           if (arr[j]<arr[j+1]) {8 ZJ=arr[j];9               Arr[j]=arr[j+1];Ten               Arr[j+1]=zj; One           } A       } -   } -   alert (arr[0]); the } - return arr; - var a=newArray (1,4,3,2,6,8); - a=paixu (a); + alert (a[0]);

JavaScript package Features at a glance:

Mathematical functions
1alert (Math.random ()); Returns a random number between 0-1 and 2alert (math.random () *10); Returns a random number between 1-10 and 3 alert (parseint (Math.random () *10)); Returns a random integer between 1-10
4 Alert (Math.ceil (1.2)); To take the whole
5 Alert (Math.floor (1.2)); Take down the whole
//Get Time
1 var d=new Date ();2 alert (d); Get current Time3 alert (D.gettime ()); Get time stamp4 alert (D.getfullyear ()); Get year5 alert (D.getmonth () +1); Get Month6 alert (D.getdate ()); Get Day7 alert (d.gethours ()); Get hours8 alert (D.getminutes ()); Get minutes9 alert (D.getseconds ()); Get secondsTen alert (D.getday ()); Get the day of the week OneAlert (D.getmilliseconds ()); Get milliseconds

Mathematical functions (called with math):
ABS (x) The absolute value of the return number.
Ceil (x) is rounded on the logarithm.
Floor (x) is rounded down with a logarithmic.
Round (x) rounds the number to the nearest integer.
Max (x, y) returns the highest value in X and Y.
Min (x, y) returns the lowest value in X and Y.
The POW (x, y) returns the y power of X.
SQRT (x) returns the square root of the number.
Random () returns an arbitrary number between 0 and 1. ****

String functions (called with variables):

IndexOf
Returns the index (from left to right) of the first occurrence of a substring in a string. If there is no match, returns-1.
var index1 = A.indexof ("L");
INDEX1 = 2

CharAt
Returns the character at the specified position.
var Get_char = A.charat (0);
Get_char = "H"

LastIndexOf
Returns the index (right-to-left search) at the last occurrence of a substring in a string, or 1 if there are no matches.
var index1 = lastIndexOf (' l ');
INDEX1 = 3


Match
Checks if a string matches the contents of a regular expression, and returns null if there is a match.
var re = new RegExp (/^\w+$/);
var is_alpha1 = A.match (re);
IS_ALPHA1 = "Hello"
var is_alpha2 = B.match (re);
IS_ALPHA2 = null

Substring
Returns a substring of a string in which the incoming parameter is the starting and ending position.

var sub_string2 = a.substring (1,4);
Sub_string2 = "ell"

SUBSTR ********
Returns a substring of a string in which the incoming parameter is the starting position and length
var sub_string1 = a.substr (1);
Sub_string1 = "Ello"
var sub_string2 = a.substr (1,4);
Sub_string2 = "Ello"

Replace *******
Replace String, the first argument represents the replaced string, the second argument represents the replacement string
A.replace ("he", "AA")


Search
Performs a regular expression matching lookup. If the lookup succeeds, returns the matching index value in the string. otherwise returns-1.
var index1 = A.search (re);
index1 = 0
var index2 = B.search (re);
Index2 =-1

Split ******
A string is made into a string array by dividing the string into substrings.
var arr1 = A.split ("");
ARR1 = [H,e,l,l,o]

Length property *******
Returns the length of a string, which refers to the number of characters it contains.


toLowerCase
Turns the entire string into lowercase letters.
var lower_string = A.tolowercase ();
lower_string = "Hello"

toUpperCase
Turns the entire string into uppercase.
var upper_string = A.touppercase ();
upper_string = "HELLO"

JavaScript functions and JS package features at a glance

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.