JS Fourth day Study Summary:

Source: Internet
Author: User
Tags floor function local time

(1) A summary of the four forms of the function:

 no parameter no return value 
function tellstory () {
    console . Log ("formerly a Mountain");
    console . Log ("There is a temple on the Hill");
}
console . log (Tellstory ());       //Once there was a temple on Mount Hill Undefined

No parameter has a return value
function SayHello () {
    return  "Hello";
}
console . log (SayHello ());        //Result: Hello

Parameter no return value
function SayHello (name) {
   console . log ("Hello" +name);
}
console . log (SayHello ("Zhang San"));    //results: Hello Zhang San   undefined
has a parameter return value
Function Getmax (A, b) {
    if (a>b) {
        return a ;
   }else{
        return b;
   
}
console . log (Getmax (4,6));   //results: 6

Let's summarize the return value in the function:

1. If the function does not use the return statement, then the function also has a default return value: Undefined, such as the first case tellstory (), and the third case has no return value of the parameter.

2. If the function uses the return statement, then the value following the return will be the return value of the function, 2nd, 42 cases.

3. If the function uses the return statement, but there is no value after return, then the return value of the function is also: undefined

4. After the function uses the return statement, the function stops and exits immediately after executing the return statement, which means that all other code after the return is no longer executed.

(2) The so-called overload is the function name is the same, but the number of parameters of the function is different, or the parameter data type is different, which constitutes the function of overloading. There are no overloads of functions in JS.

<script>

function Getsum (A, b) {

return a+b;

}

function Getsum (a,b,c) {

return a+b+c;

}

Console.log (Getsum (2,3,4)); 9

Console.log (Getsum (2,3))//Because C is undefined, the result is Nan

</script>

(3) Scope of function variables

There is no block-level scope for JS. A block-level scope is a range of parcels between {}.

Global scope:

Variables in global scope can be accessed anywhere

1. Variables within the global scope are global variables, variables defined in the global scope are global variables that can be accessed anywhere in the page (including inside the function).

2. Variables defined within the function that do not use Var are also global variables.

Local scope: A function is a local scope, and a variable within a local scope is called a local variable.

(4) Built-in objects:

The built-in objects of the array:

<script>

ToString (), ValueOf (), join (), concat ()

var arr1=["abc", 123,567];

var arr2=["CDE", 345,678,890];

Console.log (Arr1.tostring ()); into a string, and the items in the array are separated by commas, and the join is called internally

Console.log (Arr1.valueof ()); Print the object itself

Console.log (Arr1.join ("|")); Converts an array of strings into a string output, and you can add some new characters to the array

Console.log (Arr1.concat (ARR2)); Joins another array, generating a new array.

Slice () intercepts a new array from the current array, without affecting the original array,

var arr=["123", "234", "345", "456", "567", "abc", "BCD", "CDE"];

Console.log (Arr.slice (3)); Extracts the array data backward from the array subscript 3

Parameter start starting from 0, end starting from 1

Console.log (Arr.slice (2,4)); Extracts array data from array subscript 3 to array subscript 4

Splice () deletes or replaces some items of the current array, parameters

Console.log (Arr.splice (2,1)); Delete array Data "345" labeled 2

Start,deletecount,options (the item to be replaced),

Start position (subscript) the element to be replaced by the number of deleted elements

Console.log (Arr.splice (2,4)); Delete the array data from the array subscript 2 starting with the next 4.

</script>

<script>

var arr=[1,2,3,4,5,2,6,7];

var num=arr.push ("Zxc", 123,43,54);

Console.log (num);

Console.log (arr);

Adding elements to an array

var arr2=arr.pop (); Deletes the last item of the array, returning the deleted data

Console.log (ARR2);

Console.log (arr);

var arr3=arr.shift ();//delete the first item in the array and return the deleted data

Console.log (ARR3);

Console.log (arr);

var arr4=arr.unshift ("abc", "Mmm", "999");//Add data from the front of the array and return the length of the added array

Console.log (ARR4);

Console.log (arr);

Console.log (Arr.indexof (2));//Retrieving from a go, retrieving a value of 2 returns the array subscript

Console.log (Arr.lastindexof (2));//Retrieve from backward, retrieve 2 value return array subscript

</script>

Math common built-in objects:

<script>

var a=math.pow (2,10);

Console.log (a); 1024

Console.log (Math.pow (2,5)); 32 exponentiation

Console.log (Math.Round (5.12)); 5 rounding

Console.log (Math.ceil (5.39)); 6 Ceiling Function Rounding up

Console.log (Math.floor (5.39)); 5 Floor function down rounding

Console.log (Math.Abs (3)); 3 Seeking Absolute value

Console.log (Math.Abs (-3)); 3

Console.log (Math.max (12,11,34)); 34 Seeking Maximum Value

Console.log (Math.min (44,23,12,56)); 12 to find the minimum value

Console.log (Math.random ()); Generates a random number of [0,1]

Console.log (Math.PI); A property in math

</script>

Date object:

<script>

GetTime () returns the same number of milliseconds as the valueof () result

Getmilliseconds ()

Getseconds () returns 0-59

Getminutes () returns 0-59

GetHours () returns 0-23

GetDay () returns day of the week 0 Sunday 6 weeks 6 0--6

GetDate () returns the day ordinal of the current month, the date of the month

GetMonth () returns the month, starting from 0

getFullYear () returns a 4-bit year, such as 2016

var date=new date ();

Console.log (date);

Console.log (Date.tostring ()); return string

Console.log (Date.valueof ()); You can return the original value of a Boolean object.

Console.log (Date.todatestring ()); Converts the date part of a DateTime object to a string and returns the result.

Console.log (Date.tolocaledatestring ()); The date part of a Date object can be converted to a string based on local time, and the result is returned.

Console.log (Date.totimestring ()); You can convert the time portion of a Date object to a string and return the result

</script>

JS Fourth day Study Summary:

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.