JS Basics--Defining the data type mathematical time function array

Source: Internet
Author: User
Tags arithmetic array length mathematical functions number sign terminates

    • Defined

Event-and object-driven, and security-performance scripting language that runs on the client.

    • Reference

Inline: anywhere in HTML <script type= "Text/javascript" > Specific JS code </script>

external: <script type= "Text/javascript" src= "JS file" ></script>

    • Code case Sensitive: "A" and "a" are not a type; single-line comment//; multiline comment with/* */.
    • Output syntax

Pop-up Window form alert ();

Alert ("11111");


Console.log (); console output, typically as a debug output statement

Console.log ("123")


The prompt () method is used to display a dialog box that prompts the user for input

Prompt ("Please enter");


document.write () print in an HTML document

document.write ("Hello");

    • Variable

Definition: The amount whose value can change is the variable.

Name: var variable name =

Naming specification: Letters, numbers, underscores, $ symbols, etc. 4 components, the number can not be the beginning of the name of the content. Hump Method Name: The first word starts with a lowercase letter, and the first letter of each word starts with a capital letter from the beginning of the second word.

    • Data type

There are 6 kinds: Numeric number (Int/float) character string Boolean Boolean not defined undefined object-oriented (array is part of object) NULL type: null object type.
TypeOf (): Type of output variable

var a = 10;var B = "ASD"; alert (typeof (a)); alert (typeof (b));


Conversion of data types to each other
1) Number conversion:

Attention:

A) If the converted content itself is a string of numeric type, it will return itself in the future at the time of conversion.

b) If the converted content itself is not a string of numeric type, then the result is Nan at the time of conversion.

c) If the content to be converted is an empty string, that with the result of the conversion is 0.

D) If it is a different character, the result will be nan at the time of conversion.

2) parseint ():

A) ignore the whitespace in front of the string until the first non-null character is found, and the non-numeric string followed by the number is removed.

b) If the first character is not a number sign or minus sign, return Nan

c) The decimal is rounded. (Rounded down)

3) parsefloat ();//floating-point number (decimal)

As with parseint, the only difference is that parsefloat can keep decimals.

B. Transfer string

Other data types can be converted to strings.

1) String ():

2) toString () method to convert (wrapper class). Note: Undefined,null cannot be used with ToString.

    • Mathematical functions

Defines the execution of normal arithmetic tasks.
The random number Math.random () returns a random number from 0 to 1.
Maximum number Math.max Returns the number of the specified number with a larger value
The minimum number math.min returns the number of the specified number with a larger value
Pi Math.PI

    • Date-time functions

Get current time: var mydate = new Date ();//Get System Current time

var mydate = new Date (); alert (mydate);

Get Month Day
1 mydate.getyear (); Gets the value of the current year-1900 (3-bit)
2 Mydate.getfullyear (); Get the full year (4-bit, 1970-????)
3 Mydate.getmonth (); Get the current month (0-11, 0 for January)
4 Mydate.getdate (); Get current day (1-31)
5 Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
6 mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1)
7 mydate.gethours (); Get current number of hours (0-23)
8 Mydate.getminutes (); Gets the current number of minutes (0-59)
9 Mydate.getseconds (); Gets the current number of seconds (0-59)
Ten mydate.getmilliseconds (); Gets the current number of milliseconds (0-999)
Mydate.tolocaledatestring (); Get Current date
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time

var mydate = new Date (); var y=mydate.getfullyear (); Get the full year (4-bit, 1970-????) var m= mydate.getmonth (); Gets the current month (0-11, 0 for January) var d= mydate.getdate (); Gets the current day (1-31) var w= mydate.getday (); Get Current week x (0-6, 0 for Sunday) var h= mydate.gethours (); Gets the current number of hours (0-23) var f= mydate.getminutes (); Gets the current number of minutes (0-59) var s= mydate.getseconds (); Gets the current number of seconds (0-59) var i= mydate.tolocaledatestring (); Get current date var j= mytime=mydate.tolocaletimestring (); Get current time Var k= mydate.tolocalestring (); Get date and Time alert (y+ "-" +m+ "-" +d+ "-" +w+ "-" +h+ "-" +f+ "-" +s "; alert (i); alert (j); alert (k);
    • Operator

Arithmetic operator: +;-;*;/

+:1 Number summation 2 concatenation of strings

var i = 10;var j = 10;alert (i+j);

var i = "Hello"; var j = "Hello World"; alert (I+J);

-:1 Digit Subtraction 2 logarithm of inverse number 3 converts a string to a numeric numeric string-numeric

*: Multiply;/: except;%: Withdraw

Compound assignment operator = =; *=;/=;%=

(sum+=a) = =  Sum=sum+a

Self-increment and self-reduction

+ + increments the number of operations;--decrements the number of operands

Increment or decrement, and then assign, before the operand, to assign, increment, or decrement after the operand.

Relational operators

Size relationship detection: greater than >; equal to >=; less than <; less than or equal to <=

Equivalence relationship detection: equals = =; not equals! =

Logical operator: logical non! Logic and && logic or | |

Ternary operators

var a =10;var b =10;var c =a>b? " A greater than B ": A==b?" A is equal to B ":" A is smaller than B "alert (c);

    • Process statements

SELECT statement

If statement attributes: Handles complex logical relationships

if (condition 1) {Code 1}else if (condition 2) {Code 2}else{Code 3}

Switch statement flow: 1 To determine the value of the conditional expression 2 is compared with the label; properties: Handling Simple logic

Switch (1==1) {case true:  alert (111) break;case False:alert (222) break; defaultbreak;}

Looping statements

While statement flow: The value of the conditional expression is judged, the execution code is executed when the values are true, and the loop is exited when the duty is false. Characteristics: Check the condition first, then execute the loop; the loop does not execute once the condition is not satisfied

while (conditional expression) {  loop-executed code}

Do-while Statement Flow: Execute the Loop body code First, then judge if the expression.  True to repeat the execution of the code if the expression is false to exit the loop. Characteristics: The loop body is executed first, then the condition is judged; the Loop body code executes at least once

do{  Code}while (conditional expression) for loop execution;

For statement flow: The loop variable initial value is compared with the loop condition, and when the return value returns True, the execution of the loop body executes once, the loop body is incremented or decremented, and the result of the operation is compared with the cyclic condition.

For (defines the initial value of the loop variable; loop condition; increment decrement counter) {   Loop code}

Jump statement

Break: Terminates the entire loop, no longer judging

Continue: Terminates this cycle and then determines whether the next loop is executed

    • Array

Definition: An array uses a separate variable name to store a series of values.

Defining an empty array

var variable name = new Array (), var variable name = [];var Variable name = new Array (length of array);

Adding elements to an array

1 Add the 2 push method by adding the subscript one by one or by looping

var A=[1,2,0,5,6,0,4,8,2];var attr2 = [];    Define an empty array//iterate through the array, remove the same elements from the a array, and fill in the ATTR2 array for the for (Var i= 0;i<a.length;i++) {if (a[i]== 0) Continue;attr2.push (A[i]);} alert (ATTR2);

Traversing an array through a for loop or for in

var Xvar mycars = new Array () mycars[0] = "Saab" mycars[1] = "Volvo" mycars[2] = "BMW" for (x in Mycars) {document.write (mycars [x] + "<br/>")}

Built-in operator functions for arrays

Add a value at the end of an array

var arr=new Array (1,2,3,4,5), Var len=arr.push (7,9); Console.log (Len,arr);

Add a value at the beginning of the array

var arr=new Array (1,2,3,4,5), Var len=arr.unshift (7,9); Console.log (Len,arr);

Delete the value of an array pop deletes the last one

<script type= "Text/javascript" >var arr = new Array (1,2,3,4,5); var val = Arr.pop (); Console.log (arr); Console.log ( Val);</script>

  

Shift vs. Pop Delete the value labeled 0

<script type= "Text/javascript" >var arr = new Array (1,2,3,4,5); var val = Arr.shift (); Console.log (arr); Console.log (Val);</script>

  

Array into a string join

<script type= "Text/javascript" >var arr = new Array (1,2,3,4,5); var str = Arr.join ("."); Console.log (str);</script>

  

Sort of array reverse reverse sort

<script type= "Text/javascript" >var arr = new Array (1,2,3,45,5); Arr.reverse () Console.log (arr);</script>

  

var arr=new Array (1,2,3,4,5,59,6);
Arr.sort ();
Console.log (arr);

Note: sort () defaults to the conversion string reordering so it cannot be sorted by number size

Sort by number size

<script type= "Text/javascript" >var arr=new Array (1,2,3,4,5,59,6);  Arr.sort (function (A, b) {return-A-b}); Ascending    Console.log (arr);    Arr.sort (function (A, b) {return b-a});//Descending    Console.log (arr);</script>

  

Connection array

var arr1=[1,2,3];    var arr2=[4,5,6];    var arr3=[7,8,9];    var arr=arr1.concat (arr2,arr3,[10,11]);    Console.log (arr);

  

For the interception of arrays slice

    var arr=new Array (1,2,3,4,5);    var arr2=arr.slice (2);    The first parameter start selects the index subscript value 2 for the third value (0,1,2) Number 3 begins with an optional default to the end of the array    console.log (ARR2);//Output [3, 4, 5]    //Note the end parameter is the The parameter is the array fragment at the end of arrays subscript 4 is the number 5 of the subscript 5 before interception, you can also intercept to End-1    var arr2=arr.slice (2,4);    Console.log (ARR2);//Output [3, 4]  is not [3,4,5]    //intercept starts with a negative    var arr2=arr.slice ( -2,4);    If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.    ///You can also convert an array length (5) plus a negative value (-2) equivalent to Arr.slice (3,4);    Console.log (ARR2);//Output [4]

  

Deletion and insertion of arrays splice

    Delete    var arr=new Array (1,2,3,4,5);    var arr2=arr.splice (+//) Delete the value starting with 1 (2) starts with 2 values (2 and 3) to return the deleted value    console.log (ARR,ARR2);    Insert    var arr=new Array (1,2,3,4,5);    var arr2=arr.splice (1,0,11,111);//Insert a value before the value of subscript 1 (2), the second of the parameter is 0 not deleted, insert 11,111    console.log (arr);    var arr=new Array (1,2,3,4,5);    var arr2=arr.splice (1,1,11,111);//Insert a value before the value of subscript 1 (2), delete the value labeled 1 and insert the 11,111    console.log (arr);    var arr=new Array (1,2,3,4,5);    var arr2=arr.splice (1,2,11,111);//Insert a value before the value of subscript 1 (2), delete the value of subscript 1 and 2, and insert the 11,111    console.log (arr);

  

Find index of

Find the subscript    var arr=new Array (1,2,3,4,5,6,7,3) where the value is located;    var index=arr.indexof (3);    Console.log (index);  Detects where the first 3 appears    var index=arr.indexof (3,4), the second argument starts with the value    console.log (index), and detects the position of the first 3 starting with the 4 (number 5 start)    var Index=arr.indexof (ten);    Console.log (index);    Start looking for lastIndexOf ()    var index=arr.lastindexof (3) at the end;    

  

JS Basics--Defining the data type mathematical time function array

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.