JavaScript
Quotes
let single = 'single-quoted';
let double = "double-quoted";
let backticks = `backticks`;
${}
function sum(a, b) {
return a + b;
}
alert(`1 + 2 = ${sum(1, 2)}.`); // 1 + 2 = 3.
let guestList = `Guests:
* John
* Pete
* Mary
`;
alert(guestList); //
let guestList = "Guests: // Error: Unexpected token ILLEGAL
* John";
func`string` func docs tagged templates
newline character \n
let guestList = "Guests:\n * John\n * Pete\n * Mary";
alert(guestList); //
let str1 = "Hello\nWorld"; //
//
let str2 = `Hello
World`;
alert(str1 == str2); // true
\n |
|
\r |
Windows \r\n Windows \n Windows \n |
\', \" |
|
\\ |
|
\t |
|
\b, \f, \v |
|
\xXX |
Unicode XX Unicode '\x7A' 'z' |
\uXXXX |
UTF-16 XXXX Unicode \u00A9 Unicode 4 |
\u{XXXXXXX}1 6 |
UTF-32 Unicode Unicode 4 |
Unicode
alert( "\u00A9" ); //
alert( "\u{20331}" ); // Unicode
alert( "\u{1F60D}" ); // Unicode
\
alert( 'I\'m the Walrus!' ); // I'm the Walrus!
\'
alert( `I'm the Walrus!` ); // I'm the Walrus!
\ JavaScript \ alert
\
\\
alert( `The backslash: \\` ); // The backslash: \
length
alert( `My\n`.length ); // 3
\n 3
length str.length() str.length
str.length
pos [pos] str.charAt(pos)
let str = `Hello`;
//
alert( str[0] ); // H
alert( str.charAt(0) ); // H
//
alert( str[str.length - 1] ); // o
charAt
[] undefined charAt
let str = `Hello`;
alert( str[1000] ); // undefined
alert( str.charAt(1000) ); // ''
for..of
for (let char of "Hello") {
alert(char); // H,e,l,l,ochar "H" "e" "l"
}
JavaScript
let str = 'Hi';
str[0] = 'h'; // error
alert( str[0] ); //
str
let str = 'Hi';
str = 'h' + str[1]; //
alert( str ); // hi
alert( 'Interface'.toUpperCase() ); // INTERFACE
alert( 'Interface'.toLowerCase() ); // interface
alert( 'Interface'[0].toLowerCase() ); // 'i'
str.indexOf
pos str substr -1
let str = 'Widget with id';
alert( str.indexOf('Widget') ); // 0 'Widget'
alert( str.indexOf('widget') ); // -1
alert( str.indexOf("id") ); // 1"id" 1 idget id
"id" 1 2
let str = 'Widget with id';
alert( str.indexOf('id', 2) ) // 12
indexOf
let str = 'As sly as a fox, as strong as an ox';
let target = 'as'; //
let pos = 0;
while (true) {
let foundPos = str.indexOf(target, pos);
if (foundPos == -1) break;
alert( `Found at ${foundPos}` );
pos = foundPos + 1; //
}
let 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 );
}
str.lastIndexOf(substr, pos) if indexOf if
let str = "Widget with id";
if (str.indexOf("Widget")) {
alert("We found it"); //
}
alert str.indexOf("Widget") 0 if 0 false
-1
let str = "Widget with id";
if (str.indexOf("Widget") != -1) {
alert("We found it"); //
}
bitwiseNOT
bitwise NOT ~ 32-bit
32-bit ~n -(n+1)
alert( ~2 ); // -3 -(2+1)
alert( ~1 ); // -2 -(1+1)
alert( ~0 ); // -1 -(0+1)
alert( ~-1 ); // 0 -(-1+1)
n == -1 ~n 32-bit n
indexOf -1 if ( ~str.indexOf("...") )
indexOf
let str = "Widget";
if (~str.indexOf("Widget")) {
alert( 'Found it!' ); //
}
if (~str.indexOf(...)) if found
~ 32 0 ~4294967295=0
JavaScript .includes
includesstartsWithendsWith
str.includes(substr, pos) str substr true/false
alert( "Widget with id".includes("Widget") ); // true
alert( "Hello".includes("Bye") ); // false
str.includes
alert( "Widget".includes("id") ); // true
alert( "Widget".includes("id", 3) ); // false, 3 "id"
alert( "Widget".startsWith("Wid") ); // true"Widget" "Wid"
alert( "Widget".endsWith("get") ); // true"Widget" "get"
JavaScript substringsubstr slice
str.slice(start [, end])-
startendlet str = "stringify"; alert( str.slice(0, 5) ); // 'strin' 0 5 5 alert( str.slice(0, 1) ); // 's' 0 1 1 0slicelet str = "stringify"; alert( str.slice(2) ); //start/endlet str = "stringify"; // alert( str.slice(-4, -1) ); // 'gif' str.substring(start [, end])-
startendslicestartendlet str = "stringify"; // substring alert( str.substring(2, 6) ); // "ring" alert( str.substring(6, 2) ); // "ring" // slice alert( str.slice(2, 6) ); // "ring" alert( str.slice(6, 2) ); // ""slice
0 str.substr(start [, length])-
startlengthlengthlet str = "stringify"; alert( str.substr(2, 4) ); // 'ring' 2 4let str = "stringify"; alert( str.substr(-4, 2) ); // 'gi' 4 2
slice(start, end) |
start end end |
|
substring(start, end) |
start end end |
0 |
substr(start, length) |
start length |
start |
substr JavaScript B B substr
slice slice
-
alert( 'a' > 'Z' ); // true -
alert( 'sterreich' > 'Zealand' ); // trueZealandsterreich
JavaScript
str.codePointAt(pos)-
pos:// alert( "z".codePointAt(0) ); // 122 alert( "Z".codePointAt(0) ); // 90 String.fromCodePoint(code)-
codealert( String.fromCodePoint(90) ); // Z\uUnicode// 90 5a alert( '\u005a' ); // Z
65..220
let str = '';
for (let i = 65; i <= 220; i++) {
str += String.fromCodePoint(i);
}
alert( str );
// ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
//
a > Z
a97 Z90
-
az
str.localeCompare(str2) str str2
-
strstr2 -
strstr2 -
0
alert( 'sterreich'.localeCompare('Zealand') ); // -1
Unicode
emoji
2 2
2 65536 2
2
alert( ''.length ); // 2 X
alert( ''.length ); // 2
alert( ''.length ); // 2
JavaScript
length 2
String.fromCodePoint str.codePointAt String.fromCharCode str.charCodeAt fromCodePoint/codePointAt
alert( ''[0] ); //
alert( ''[1] ); //
alert
0xd800..0xdbff 0xdc00..0xdfff
// charCodeAt
alert( ''.charCodeAt(0).toString(16) ); // d835 0xd800 0xdbff
alert( ''.charCodeAt(1).toString(16) ); // dcb3, 0xdc00 0xdfff
/
a UTF-16
UTF-16 Unicode
S dot above \u0307 S
alert( 'S\u0307' ); // S
dot below \u0323S S
alert( 'S\u0307\u0323' ); // S
Unicode
let s1 = 'S\u0307\u0323'; // SS + +
let s2 = 'S\u0323\u0307'; // SS + +
alert( `s1: ${s1}, s2: ${s2}` );
alert( s1 == s2 ); // false?!
Unicode
alert( "S\u0307\u0323".normalize() == "S\u0323\u0307".normalize() ); // true
normalize() 3 \u1e68S
alert( "S\u0307\u0323".normalize().length ); // 1
alert( "S\u0307\u0323".normalize() == "\u1e68" ); // true
S UTF-16
Unicode Unicode
- 3
${} - JavaScript UTF-16
-
\n\u...Unicode -
[] -
slicesubstring - /
toLowerCase/toUpperCase -
indexOfincludes/startsWith/endsWith -
localeCompare
<code><pre>10 plnkrJSBincodepen