JS Basic Example

Source: Internet
Author: User

Creating variables

var Obj=value;

where obj is the variable name; The value means a number, an array, a function, etc.

Multi-variable calculation

var a1=200,b1= ' Hello ', c1=400;

var d1=c1-a1;//this is a simple calculation.

The main work of a variable is to save the right to the left of the equals sign

Console.log (obj); Enter the contents of obj, where obj means it may be an array, possibly a function.

Str.tostring () converted to string mode

typeof (obj): see what type of obj is

String (c): convert C to string

Number (x): limited to Pure digital turn

parseint (obj)/parsefloat (obj)

+ + before the first operation, to the new value

+ + in the After, first operation, to the old value

Short circuit Mode:obj&& (OBJ1)

Example

For example, whether the amount is greater than 500, more than 0.8

obj>=500&& (obj*=0.8)

Set default value: Value 1| | Value 2

Example

Console.log (reply| | ' Text ')

  

Trinocular/Ternary

An expression? Action 1: Action 2

Example

A>1?consoel.log (a): 1

Strong ternary

An expression? Action 1: Expression? Action 2: Expression? Action 3: Default value;

Example

A>=2000> ' A ': a<7000? ' A+1 ': ' Hell ';

  

Function Surface Questions

Function F1 () {1}

F1 ();

Function F1 () {2}

F1 ()

Note: If F1 () appears multiple times on the page, the previous function is overwritten

Result output 2,2

var f1= ';

F1 ();//error, because the variable name will never be a function

Declaring an advance issue

var n = 100;
function func () {
var n; Declaration in advance
Console.log (n);//?undefined
n = 99;
Console.log (n);//?99
}
Func ();
Console.log (n); ? 100

  

Code: encodeURI (s); Decode: decodeURI (s)

Calculation or program

Eval (obj)

Switch (obj) {///Note: The expression in switch () brackets indicates that the = = = is all equal to the way

Case OBJ+1: statement body; break

Default

Statement body

}

var num = parseint (Prompt ("please button"));
Switch (num) {
Case 1:console.log ("in Query ...");
Case 2:
Case 3:console.log ("System maintenance:"); Break
Case 0:console.log ("Please Hang Up");
Default:console.log ("Invalid button");
}

Calculate the day ordinal of the current month

1: Write a function that determines if it is a leap year

function Getleapyear (y) {
if (y%4==0 && y%100!=0 | | (y%400==0)) {
return true;
}else{
return false;
}
}

2: Write a function for the current month

function GetDays (y,m,d) {

var x=getleapyear (y);

var s=x==true?29:28;

var days=0;

By comparing M with the past month, we conclude that the following

m>1&& (DAYS=DAYS+31);

m>1&& (Days=days+s);

... Written to around November

Days+=d;

Return days

}

Cycle examples

Sum

/1: Create variable save cumulative result sum 0
2: Create loop variable save start value I 2
3: Create loop Add judgment condition <= 100
4: Accumulate in the loop body

var sum=0;

for (Var i=1;i<=100;i++) {

Sum+=i

}sum

Ask 2+4+6+. Example of +100

for (Var i=2;i<=100;i+=2) {

The operation here is the same as the sum.

}

Compare size

function d (a,b,c) {

var max=a;

b>max&& (MAX=B);

c>max&& (MAX=C);

return Max;

}

Compare size in array

Function d (a) {

var max=a[0];

if (A[i]>max) {

Max=a[i]

}

  

}

Determine if it is a letter, number, or other

if (a>= ' a ' && a<= ' z ') {}

if (a>=0&& a<=0) {}

else{}

Array comparison size

Arr.sort (function (b) {

Return A-B

})

Determine how many leap years between 2000 and 3000

1: The push () mode of the array

2: By creating a variable COUNRT

Then in judging leap year if under count++

Example

var count = 0;
for (Var i=2000;i<=3000;i++) {
if (i%4==0 && i%100!=0 | | (i%400==0)) {
count++;
}
}
Console.log (count);//243

When the number of statistics

var num = 0;//Save number
var CHS = 0;//Saves the number of letters 16:54----17:05
var cc = 0;
var other = 0;
for (Var i=0;i<str.length;i++) {
var c = Str.charat (i);
if (c >= ' 0 ' && C <= ' 9 ') {
num++;
}else if (c >= ' a ' && c <= ' z ') {
chs++;
}else if (c >= ' \u4e00 ' && c<= ' \u9fa5 ') {
cc++;
}else{
other++;
}
}
Console.log ("Number is the number is:" +num);
Console.log ("The number of letters is:" +chs);
Console.log ("The number of Chinese characters is:" +CC);
Console.log ("The other number is:" +other);

Prime

for (Var i=2;i<100;i++) {

for (Var j=2;j<i;j++) {

if (i%j==0) {

' Not prime number '

}

}

}

Mobile phone number Verification

/^ (+86|0086)? \s*1[3578]\d{9}$/

Mailbox

/^\[email protected]\w+\. [A-z0-9]+ (\.[ a-z]+)? $/

Str.replace (Regular or string, new value )

Str= ' 20160505 '

var d=/\d{4}\d{2}\d{2}/

Str.replace (d, ' $1$2$3 ');

JS Basic Example

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.