// recaplet single ='single-quoted';let double ="double-quoted";let backticks =`backticks`;// backtick can put expressionfunctionsum(a, b) {return a + b;}alert(`1 + 2 = ${sum(1,2)}.`); // 1 + 2 = 3.// span multiple lineslet guestList =`Guests: * John * Pete * Mary`;alert(guestList); // a list of guests, multiple lines// errorlet guestList ="Guests: // Error: Unexpected token ILLEGAL* John";
Special characters
let guestList ="Guests:\n * John\n * Pete\n * Mary";alert(guestList); // a multiline list of guestsalert( "Hello\nWorld" ); // two lines using a "newline symbol"// two lines using a normal newline and backticksalert( `HelloWorld` );
let str ='Widget with id';alert( str.indexOf('Widget') ); // 0, because 'Widget' is found at the beginningalert( str.indexOf('widget') ); // -1, not found, the search is case-sensitivealert( str.indexOf("id") ); // 1, "id" is found at the position 1 (..idget with id)let str ='Widget with id';alert( str.indexOf('id',2) ) // 12// use in looplet str ='As sly as a fox, as strong as an ox';let target ='as'; // let's look for itlet pos =0;while (true) {let foundPos =str.indexOf(target, pos);if (foundPos ==-1) break;alert( `Found at ${foundPos}` ); pos = foundPos +1; // continue the search from the next position}// shorterlet str ="As sly as a fox, as strong as an ox";let target ="as";let pos =-1;while ((pos =str.indexOf(target, pos +1)) !=-1) {alert( pos );}// 不能直接當判斷式let str ="Widget with id";if (str.indexOf("Widget")) {alert("We found it"); // doesn't work!}// 要換另一種寫法let str ="Widget with id";if (str.indexOf("Widget") !=-1) {alert("We found it"); // works now!}
alert( ~2 ); // -3, the same as -(2+1)alert( ~1 ); // -2, the same as -(1+1)alert( ~0 ); // -1, the same as -(0+1)alert( ~-1 ); // 0, the same as -(-1+1)// 在老的程式碼使用這樣的方法當判斷式let str ="Widget";if (~str.indexOf("Widget")) {alert( 'Found it!' ); // works}
includes, startsWith, endsWith 檢查字串是否有子字串,返回布林值。
// includesalert( "Widget with id".includes("Widget") ); // truealert( "Hello".includes("Bye") ); // falsealert( "Midget".includes("id") ); // truealert( "Midget".includes("id",3) ); // false, from position 3 there is no "id"// startsWith & endsWithalert( "Widget".startsWith("Wid") ); // true, "Widget" starts with "Wid"alert( "Widget".endsWith("get") ); // true, "Widget" ends with "get"
Getting a substring
str.slice(start [, end]) 返回開始點到結束點的部分字串
let str ="stringify";alert( str.slice(0,5) ); // 'strin', the substring from 0 to 5 (not including 5)alert( str.slice(0,1) ); // 's', from 0 to 1, but not including 1, so only character at 0// 沒有第 2 個參數直接到結尾let str ="stringify";alert( str.slice(2) ); // ringify, from the 2nd position till the end// 位置為負數也可以let str ="stringify";// start at the 4th position from the right, end at the 1st from the rightalert( str.slice(-4,-1) ); // gif
let str ="stringify";// these are same for substringalert( str.substring(2,6) ); // "ring"alert( str.substring(6,2) ); // "ring"// ...but not for slice:alert( str.slice(2,6) ); // "ring" (the same)alert( str.slice(6,2) ); // "" (an empty string)
str.substr(start [, length]) 返回從開始點計算長度的字串
let str ="stringify";alert( str.substr(2,4) ); // ring, from the 2nd position get 4 characterslet str ="stringify";alert( str.substr(-4,2) ); // gi, from the 4th position get 2 characters