JS operator, control flow, circulation, the string / array Method

Operators

  • Arithmetic operators: +, -, *, /,%, +, -
  • Assignment operator: =, + =, - =, * =, / =,% =
  • Comparison operators:>,> =, <, <=, =, ==, === (congruent, also to compare data type)!
  • Logical operators: && (and), || (or),! (Non)

Process Control

  • In making this judgment, when the following is false
    • 0
    • null
    • undefined
    • NaN
    • "" Empty string
    • false
  • if else
if (...) {
    ...
} else if(...) {
    ...
} else {
    ...
}
// 三目运算符
name ? console.log("存在:"+name) :console.log("不存在")
//多条件下的三目运算符
0 ? console.log(1) : 3>4 ? console.log(2) : console.log(3)
  • switch
var choice = 1;
switch (choice) {
    case 1:
        document.write(1);
        //不写break时,当满足条件是,不再判断,会执行后面所有语句
        break;
    case 2:
        document.write(2);
        break;
    default:
        document.write("都不满足");
}


cycle

  • for loop
//九九乘法表
for (i=1; i<=9; i++){
    for(j=1; j<=i; j++){
        document.write(j+"*"+i+"="+i*j+'&emsp;');
    }
    document.write("<br>");
}
  • while loop
while (true){
    document.write(1);
    break;
}
  • do while loop
do{
    document.write(1);
}while (1<0)


String Methods

  • Length: length
  • Index: [] / charAt ()
  • Replace: replace ()
  • Split: split ()
  • Uppercase: toUpperCase ()
  • Lowercase: toLowerCase ()
  • Index: indexOf ()
  • Interception: substring ()
  • Cutting: slice ()
var str_test = "Which Love Love";
document.write(str_test.length+'<br>');//length
document.write(str_test[0]+'<br>');//[]
document.write(str_test.charAt(1)+'<br>');//charAt()
document.write(str_test.replace("Love","===")+'<br>');//replace()
document.write(str_test.split(' ')+'<br>');//split()
document.write(str_test.toUpperCase()+'<br>');//toUpperCase()
document.write(str_test.toLowerCase()+'<br>');//toLowerCase()
document.write(str_test.indexOf('L',8)+'<br>');//indexOf()
//slice()不会自动比较参数的大小
document.write(str_test.slice(0,5)+'<br>');//slice()
//substring()会自动比较参数的大小
document.write(str_test.substring(5,0)+'<br>');//substring()


Array Methods

  • Length: length
  • Index: []
  • Additional: push ()
  • Add: unshift ()
  • After deletion: pop ()
  • Before deletion: shift ()
  • Index: indexOf ()
  • Slice: slice ()
  • Replace: splice ()
  • Stitching: join ()
  • Sort: sort ()
  • Reverse: reverse ()
  • Connection: concat ()
var arr = ['one','two','three','four','five'];
document.write(arr.length+'<br>');//length
document.write(arr[0]+'<br>');//[]
arr.push('six');//push()
arr.unshift('zero');//Unshift(),从前面添加
arr.pop();//pop()
arr.shift();//shift()
document.write(arr.indexOf('two')+'<br>');//indexOf()
document.write(arr.slice(0,3)+'<br>');//slice()
//将新的值,替换从下标为1的开始的两个值
document.write(arr.splice(1,2,'a','b','c','d')+'<br>');//splice()
document.write(arr.join('==')+'<br>');//join()
document.write(arr.sort()+'<br>');//sort()
document.write(arr.reverse()+'<br>');//reverse()
var arr_temp1=[1,2];
var arr_temp2=[3,4];
document.write(arr.concat(arr_temp2,arr_temp1)+'<br>');//concat()


Other methods

  • Into a string: toString ()
  • Rounds: toFixed ()
  • Judgment NaN: isNaN ()
  • Analyzing array: Array.isArray ()
  • Integer: parseInt ()
  • Float: parseFlost ()
  • Digital: Number ()
var num = 12.8;
var a ='0x1234';
var b ='343.324';
document.write(typeof toString(num)+'<br>');//toString()
document.write(num.toFixed()+'<br>');//toFixed()
document.write(isNaN(num)+'<br>');//isNaN()
document.write(Array.isArray(num)+'<br>');//isArray()
document.write(parseInt(a)+'<br>');//parseInt()
document.write(parseFloat(b)+'<br>');//parseFloat()
document.write(Number(b)+'<br>');//Number()




Guess you like

Origin www.cnblogs.com/jiyu-hlzy/p/12031228.html