await
break
case
catch
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
0
const
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
2
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
3
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
4
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
5
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
6
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
7
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
8
//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
9
//invalid
const 1a = 'hello'; // this gives an error
0
//invalid
const 1a = 'hello'; // this gives an error
1
//invalid
const 1a = 'hello'; // this gives an error
2
//invalid
const 1a = 'hello'; // this gives an error
3
//invalid
const 1a = 'hello'; // this gives an error
4
//invalid
const 1a = 'hello'; // this gives an error
5
//invalid
const 1a = 'hello'; // this gives an error
6
//invalid
const 1a = 'hello'; // this gives an error
7
//invalid
const 1a = 'hello'; // this gives an error
8
//invalid
const 1a = 'hello'; // this gives an error
9
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
0
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
1
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
2
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
3
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
4
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
5
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
6
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
7
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
8
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
9
//invalid
const new = 5; // Error! new is a keyword.
0
//invalid
const new = 5; // Error! new is a keyword.
1
//invalid
const new = 5; // Error! new is a keyword.
2
//invalid
const new = 5; // Error! new is a keyword.
3
//invalid
const new = 5; // Error! new is a keyword.
4
//invalid
const new = 5; // Error! new is a keyword.
5
//invalid
const new = 5; // Error! new is a keyword.
6
//invalid
const new = 5; // Error! new is a keyword.
7
//invalid
const new = 5; // Error! new is a keyword.
8
//invalid
const new = 5; // Error! new is a keyword.
9
const
0
const
1
Pengidentifikasi JavaScript
Identifier adalah nama yang diberikan kepada entitas seperti variabel, fungsi, kelas, dll
Aturan untuk Memberi Nama Pengidentifikasi JavaScript
Aturan untuk menulis identifier adalah
- Nama pengidentifikasi harus dimulai dengan huruf, garis bawah
const
2, atau tanda dolar const
3 Misalnya,//valid
const a = 'hello';
const _a = 'hello';
const $a = 'hello';
- Nama pengidentifikasi tidak boleh dimulai dengan angka. Misalnya,
//invalid
const 1a = 'hello'; // this gives an error
- JavaScript peka terhadap huruf besar-kecil. Jadi y dan Y adalah pengenal yang berbeda. Misalnya,
const y = "hi";
const Y = 5;
console.log(y); // hi
console.log(Y); // 5
- Kata kunci tidak dapat digunakan sebagai nama pengidentifikasi. Misalnya,
//invalid
const new = 5; // Error! new is a keyword.
Catatan. Meskipun Anda dapat memberi nama pengidentifikasi dengan cara apa pun yang Anda inginkan, sebaiknya berikan nama pengidentifikasi deskriptif
Jika Anda menggunakan pengidentifikasi untuk variabel untuk menyimpan jumlah siswa, lebih baik menggunakan siswa atau jumlah Siswa daripada x atau n
Pssst. BOOTCAMP PENGEMBANGAN WEB 2023 dimulai pada 01 FEBRUARI 2023. PENDAFTARAN SEKARANG DIBUKA untuk kursus kelompok 10 minggu ini. Pelajari dasar-dasarnya, HTML, CSS, JS, Tailwind, React, Next. js dan banyak lagi. ✨
Halaman ini menjelaskan tata bahasa leksikal JavaScript. Teks sumber JavaScript hanyalah urutan karakter — agar juru bahasa dapat memahaminya, string harus diuraikan menjadi representasi yang lebih terstruktur. Langkah awal penguraian disebut analisis leksikal, di mana teks dipindai dari kiri ke kanan dan diubah menjadi urutan elemen input atomik individual. Beberapa elemen masukan tidak signifikan bagi penafsir, dan akan dihapus setelah langkah ini — termasuk dan. Lainnya, termasuk , , , dan tanda baca (kebanyakan operator), akan digunakan untuk analisis sintaks lebih lanjut. dan komentar multibaris juga secara sintaksis tidak signifikan, tetapi memandu proses untuk membuat urutan token tertentu yang tidak valid menjadi valid
Karakter kontrol format tidak memiliki representasi visual tetapi digunakan untuk mengontrol interpretasi teks
Code pointNameAbbreviationDescriptionU+200CZero width non-joinerPlaced between characters to prevent being connected into ligatures in certain languages (Wikipedia).U+200DZero width joinerPlaced between characters that would not normally be connected in order to cause the characters to be rendered using their connected form in certain languages (Wikipedia).U+FEFFByte order markUsed at the start of the script to mark it as Unicode and the text's byte order (Wikipedia).In JavaScript source text, and are treated as parts, while (also called a zero-width no-break space when not at the start of text) is treated as .
Karakter spasi meningkatkan keterbacaan teks sumber dan memisahkan token satu sama lain. Karakter ini biasanya tidak diperlukan untuk fungsionalitas kode. Alat minifikasi sering digunakan untuk menghilangkan spasi untuk mengurangi jumlah data yang perlu ditransfer
Code pointNameAbbreviationDescriptionEscape sequenceU+0009Character tabulationHorizontal tabulation\tU+000BLine tabulationVertical tabulation\vU+000CForm feedPage breaking control character ().\fU+0020SpaceNormal spaceU+00A0No-break spaceNormal space, but no point at which a line may breakU+FEFFZero-width no-break spaceWhen not at the start of a script, the BOM marker is a normal whitespace character.OthersOther Unicode space charactersCharacters in the "Space_Separator" general categoryCatatan. Karakter dengan properti "White_Space" tetapi tidak termasuk dalam kategori umum "Space_Separator", U+0009, U+000B, dan U+000C masih diperlakukan sebagai spasi putih dalam JavaScript;
Catatan. Perubahan pada standar Unicode yang digunakan oleh mesin JavaScript dapat memengaruhi perilaku program. Misalnya, ES2016 meningkatkan referensi standar Unicode dari 5. 1 sampai 8. 0. 0, yang menyebabkan U+180E MONGOLIAN VOWEL SEPARATOR dipindahkan dari kategori "Space_Separator" ke kategori "Format (Cf)", dan menjadikannya non-spasi putih. Selanjutnya, hasil dari
function comment() {
/* console.log("Hello world!"); */
}
comment();
_1 diubah dari function comment() {
/* console.log("Hello world!"); */
}
comment();
2 menjadi function comment() {
/* console.log("Hello world!"); */
}
comment();
3Selain karakter, karakter terminator baris digunakan untuk meningkatkan keterbacaan teks sumber. Namun, dalam beberapa kasus, terminator baris dapat memengaruhi eksekusi kode JavaScript karena ada beberapa tempat yang melarangnya. Garis terminator juga mempengaruhi proses
Di luar konteks tata bahasa leksikal, ruang putih dan terminator garis sering digabungkan. Misalnya,
function comment() {
/* console.log("Hello world!"); */
}
comment();
4 menghapus semua spasi putih dan terminator garis dari awal dan akhir string. Pelacakan kelas karakter function comment() {
/* console.log("Hello world!"); */
}
comment();
5 dalam ekspresi reguler cocok dengan semua spasi putih dan terminator garisHanya poin kode Unicode berikut yang diperlakukan sebagai terminator baris dalam ECMAScript, karakter pemecah baris lainnya diperlakukan sebagai spasi putih (misalnya, Baris Berikutnya, NEL, U+0085 dianggap sebagai spasi putih)
Code pointNameAbbreviationDescriptionEscape sequenceU+000ALine FeedNew line character in UNIX systems.\nU+000DCarriage ReturnNew line character in Commodore and early Mac systems.\rU+2028Line SeparatorWikipediaU+2029Paragraph SeparatorWikipediaKomentar digunakan untuk menambahkan petunjuk, catatan, saran, atau peringatan ke kode JavaScript. Ini dapat membuatnya lebih mudah dibaca dan dipahami. Mereka juga dapat digunakan untuk menonaktifkan kode untuk mencegahnya dieksekusi;
JavaScript memiliki dua cara lama untuk menambahkan komentar ke kode. baris komentar dan blokir komentar. Selain itu, ada sintaks komentar hashbang khusus
Cara pertama adalah komentar
function comment() {
/* console.log("Hello world!"); */
}
comment();
_6; . Sebagai contohfunction comment() {
// This is a one line JavaScript comment
console.log("Hello world!");
}
comment();
Cara kedua adalah gaya
function comment() {
/* console.log("Hello world!"); */
}
comment();
_7, yang jauh lebih fleksibelMisalnya, Anda dapat menggunakannya dalam satu baris
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
Anda juga dapat membuat komentar multi-baris, seperti ini
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
Anda juga dapat menggunakannya di tengah baris, jika diinginkan, meskipun hal ini dapat membuat kode Anda lebih sulit dibaca sehingga harus digunakan dengan hati-hati
function comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
Selain itu, Anda dapat menggunakannya untuk menonaktifkan kode agar tidak berjalan, dengan membungkus kode dalam komentar, seperti ini
function comment() {
/* console.log("Hello world!"); */
}
comment();
Dalam hal ini, panggilan
function comment() {
/* console.log("Hello world!"); */
}
comment();
_8 tidak pernah dikeluarkan, karena ada di dalam komentar. Sejumlah baris kode dapat dinonaktifkan dengan cara iniBlokir komentar yang berisi setidaknya satu terminator baris berperilaku seperti di
Ada sintaks komentar khusus ketiga, komentar hashbang. Komentar hashbang berperilaku persis seperti komentar satu baris saja (
function comment() {
/* console.log("Hello world!"); */
}
comment();
6), kecuali bahwa itu dimulai dengan #!/usr/bin/env node
console.log("Hello world");
0 dan hanya valid pada awal absolut dari skrip atau modul. Perhatikan juga bahwa tidak ada spasi kosong apa pun yang diizinkan sebelum #!/usr/bin/env node
console.log("Hello world");
0. Komentar terdiri dari semua karakter setelah #!/usr/bin/env node
console.log("Hello world");
0 hingga akhir baris pertama; Komentar hashbang di JavaScript menyerupai shebang di Unix yang menyediakan jalur ke juru bahasa JavaScript tertentu yang ingin Anda gunakan untuk mengeksekusi skrip. Sebelum komentar hashbang menjadi standar, itu sudah diimplementasikan secara de-facto di host non-browser seperti Node. js, yang dihapus dari teks sumber sebelum diteruskan ke mesin. Contohnya adalah sebagai berikut
#!/usr/bin/env node
console.log("Hello world");
Penerjemah JavaScript akan memperlakukannya sebagai komentar normal — ini hanya memiliki arti semantik ke shell jika skrip dijalankan langsung di shell
Peringatan. Jika Anda ingin skrip dapat dijalankan secara langsung di lingkungan shell, enkodekan dalam UTF-8 tanpa BOM. Meskipun BOM tidak akan menyebabkan masalah untuk menjalankan kode di browser — karena BOM dihapus selama decoding UTF-8, sebelum teks sumber dianalisis — shell Unix/Linux tidak akan mengenali hashbang jika didahului oleh karakter BOM
Anda hanya boleh menggunakan gaya komentar
#!/usr/bin/env node
console.log("Hello world");
_0 untuk menentukan juru bahasa JavaScript. Dalam semua kasus lain, cukup gunakan komentar function comment() {
/* console.log("Hello world!"); */
}
comment();
_6 (atau komentar multibaris)Pengidentifikasi digunakan untuk menghubungkan nilai dengan nama. Pengenal dapat digunakan di berbagai tempat
const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
Dalam JavaScript, pengidentifikasi biasanya dibuat dari karakter alfanumerik, garis bawah (
#!/usr/bin/env node
console.log("Hello world");
5), dan tanda dolar (#!/usr/bin/env node
console.log("Hello world");
6). Pengidentifikasi tidak boleh dimulai dengan angka. Namun, pengidentifikasi JavaScript tidak hanya terbatas pada ASCII — banyak titik kode Unicode juga diperbolehkan. Yakni, karakter apa pun dalam kategori ID_Start dapat memulai pengenal, sedangkan karakter apa pun dalam kategori ID_Continue dapat muncul setelah karakter pertamaCatatan. Jika, karena alasan tertentu, Anda perlu mengurai sendiri beberapa sumber JavaScript, jangan menganggap semua pengidentifikasi mengikuti pola
#!/usr/bin/env node
console.log("Hello world");
7 (i. e. ASCII saja). Kisaran pengidentifikasi dapat dijelaskan oleh regex #!/usr/bin/env node
console.log("Hello world");
8 (tidak termasuk urutan pelolosan unicode)Selain itu, JavaScript memungkinkan penggunaan dalam bentuk
#!/usr/bin/env node
console.log("Hello world");
9 atau const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
0 dalam pengidentifikasi, yang menyandikan nilai string yang sama dengan karakter Unicode yang sebenarnya. Misalnya, const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
1 dan const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
2 adalah pengidentifikasi yang samaconst 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
Tidak semua tempat menerima rangkaian lengkap pengidentifikasi. Sintaks tertentu, seperti deklarasi fungsi, ekspresi fungsi, dan deklarasi variabel memerlukan penggunaan nama pengidentifikasi yang bukan
function import() {} // Illegal: import is a reserved word.
Terutama, properti pribadi dan properti objek mengizinkan kata-kata yang dicadangkan
const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
Kata kunci adalah token yang terlihat seperti pengidentifikasi tetapi memiliki arti khusus dalam JavaScript. Misalnya, kata kunci
const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
_3 sebelum deklarasi fungsi menunjukkan bahwa fungsi tersebut asinkronBeberapa kata kunci dicadangkan, artinya tidak dapat digunakan sebagai pengidentifikasi untuk deklarasi variabel, deklarasi fungsi, dll. Mereka sering disebut kata-kata yang dilindungi undang-undang. disediakan di bawah ini. Tidak semua kata kunci dicadangkan — misalnya,
const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
3 dapat digunakan sebagai pengidentifikasi di mana saja. Beberapa kata kunci hanya dicadangkan secara kontekstual — misalnya, const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
5 hanya dicadangkan dalam badan fungsi asinkron, dan const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
6 hanya dicadangkan dalam kode mode ketat, atau const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
7 dan const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
6 deklarasiPengidentifikasi selalu dibandingkan dengan nilai string, jadi escape sequence diinterpretasikan. Misalnya, ini masih merupakan kesalahan sintaksis
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
0Kata kunci ini tidak dapat digunakan sebagai pengidentifikasi untuk variabel, fungsi, kelas, dll. di mana saja di sumber JavaScript
const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
_9const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_0const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_1const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_2const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
7const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_4const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_5const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_6const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
7const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_8const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_9function import() {} // Illegal: import is a reserved word.
_0function import() {} // Illegal: import is a reserved word.
_1function import() {} // Illegal: import is a reserved word.
_3function import() {} // Illegal: import is a reserved word.
_4function import() {} // Illegal: import is a reserved word.
_5function import() {} // Illegal: import is a reserved word.
_6function import() {} // Illegal: import is a reserved word.
_7function import() {} // Illegal: import is a reserved word.
_8function import() {} // Illegal: import is a reserved word.
_9const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
0const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
1const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
2const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
3const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
4const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
5const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
6const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
8const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
_9function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
00function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_01function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_02function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_03
Berikut ini hanya dicadangkan jika ditemukan dalam kode mode ketat
const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
6 (juga dicadangkan di const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
7, const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
6, dan deklarasi kelas)function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_07function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
08 (juga dicadangkan di badan fungsi generator)
Berikut ini hanya dicadangkan jika ditemukan dalam kode modul atau badan fungsi asinkron
Berikut ini dicadangkan sebagai kata kunci mendatang oleh spesifikasi ECMAScript. Mereka tidak memiliki fungsi khusus saat ini, tetapi mungkin di masa mendatang, sehingga mereka tidak dapat digunakan sebagai pengidentifikasi
Ini selalu dipesan
Berikut ini hanya dicadangkan jika ditemukan dalam kode mode ketat
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
11function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
12function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
13function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
14function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
15function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
16
Future reserved words in older standards
The following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3)
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
17function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
18function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
19function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
20function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
21function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
22function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
23function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
24function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
25function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
26function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
27function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
28function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
29function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
30function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
31function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
32
Beberapa pengidentifikasi memiliki arti khusus dalam beberapa konteks tanpa kata-kata khusus apa pun. Mereka termasuk
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
33 (bukan kata kunci, tetapi tidak dapat dinyatakan sebagai pengidentifikasi dalam mode ketat)function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
34 ()const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
3function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
37 (bukan kata kunci, tetapi tidak dapat dinyatakan sebagai pengidentifikasi dalam mode ketat)- ________51______38 (________51______39)
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
40function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_41function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_42
Catatan. Bagian ini membahas literal yang merupakan token atom. Objek literal dan ekspresi yang terdiri dari serangkaian token
Lihat juga
const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
_1 untuk informasi lebih lanjutfunction comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
1Lihat juga untuk informasi lebih lanjut
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
2Jenis dan menggunakan literal numerik
Desimal
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
3Literal desimal dapat dimulai dengan nol (
function comment() {
/* console.log("Hello world!"); */
}
comment();
2) diikuti oleh digit desimal lainnya, tetapi jika semua digit setelah function comment() {
/* console.log("Hello world!"); */
}
comment();
2 di depan lebih kecil dari 8, angka tersebut ditafsirkan sebagai angka oktal. Ini dianggap sebagai sintaks warisan, dan angka literal diawali dengan function comment() {
/* console.log("Hello world!"); */
}
comment();
2, apakah ditafsirkan sebagai oktal atau desimal, menyebabkan kesalahan sintaks di — jadi, gunakan awalan function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
47 sebagai gantinyafunction comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
4Eksponensial
Literal eksponensial desimal ditentukan oleh format berikut.
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
48; function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
5Biner
Sintaks bilangan biner menggunakan nol di depan diikuti dengan huruf Latin huruf kecil atau besar "B" (
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
53 atau function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
54). Karakter apa pun setelah function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_53 yang bukan 0 atau 1 akan mengakhiri urutan literalfunction comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
6Oktal
Sintaks angka oktal menggunakan nol di depan diikuti dengan huruf Latin huruf kecil atau besar "O" (
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
47 atau function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
57. Karakter apa pun setelah function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_47 yang berada di luar rentang (01234567) akan menghentikan urutan literalfunction comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
7Heksadesimal
Sintaks angka heksadesimal menggunakan nol di depan diikuti dengan huruf Latin kecil atau besar "X" (
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
59 atau function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
60). Karakter apa pun setelah function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_59 yang berada di luar rentang (0123456789ABCDEF) akan menghentikan urutan literalfunction comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
8BigInt literal
Jenisnya adalah primitif numerik dalam JavaScript yang dapat mewakili bilangan bulat dengan presisi sewenang-wenang. Literal BigInt dibuat dengan menambahkan
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
62 ke akhir bilangan bulatfunction comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_9Literal BigInt tidak boleh dimulai dengan
function comment() {
/* console.log("Hello world!"); */
}
comment();
2 untuk menghindari kebingungan dengan literal oktal lamafunction comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_0Untuk angka oktal
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
64, selalu gunakan nol diikuti dengan huruf "o" (huruf besar atau kecil)function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_1Untuk informasi lebih lanjut tentang
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_64, lihat jugaPemisah numerik
Untuk meningkatkan keterbacaan literal numerik, garis bawah (
#!/usr/bin/env node
console.log("Hello world");
5, function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
67) dapat digunakan sebagai pemisahfunction comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_2Perhatikan batasan ini
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_3Literal adalah nol atau lebih titik kode Unicode yang diapit tanda kutip tunggal atau ganda. Poin kode Unicode juga dapat diwakili oleh urutan pelarian. Semua poin kode dapat muncul secara harfiah dalam string literal kecuali untuk poin kode kutipan penutup ini
- U+005C \ (garis miring terbalik),
- U+000D ,
- and U+000A .
Poin kode apa pun dapat muncul dalam bentuk urutan pelarian. Literal string mengevaluasi ke nilai String ECMAScript. Saat membuat nilai String ini, poin kode Unicode dikodekan UTF-16
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_4Urutan pelarian heksadesimal
Urutan escape heksadesimal terdiri dari
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
68 diikuti dengan tepat dua digit heksadesimal yang mewakili unit kode atau titik kode dalam rentang 0x0000 hingga 0x00FFfunction comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
5Urutan pelarian Unicode
Urutan pelolosan Unicode terdiri dari tepat empat digit heksadesimal setelah
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
69. Ini mewakili unit kode dalam pengkodean UTF-16. Untuk titik kode U+0000 hingga U+FFFF, satuan kode sama dengan titik kode. Poin kode U+10000 hingga U+10FFFF memerlukan dua escape sequence yang mewakili dua unit kode (pasangan pengganti) yang digunakan untuk menyandikan karakter; Lihat juga
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
70 dan function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
71function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_6Titik kode Unicode lolos
Pelarian titik kode Unicode terdiri dari
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
72, diikuti oleh titik kode dalam basis heksadesimal, diikuti oleh function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
73. Nilai digit heksadesimal harus dalam rentang 0 dan 0x10FFFF inklusif. Poin kode dalam rentang U+10000 hingga U+10FFFF tidak perlu direpresentasikan sebagai pasangan penggantiLihat juga
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
74 dan function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
75function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_7Literal ekspresi reguler diapit oleh dua garis miring ke depan (
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
76). Lexer menggunakan semua karakter hingga garis miring berikutnya yang tidak lolos atau akhir baris, kecuali garis miring muncul dalam kelas karakter (function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
77). Beberapa karakter (yaitu, yang ) dapat muncul setelah garis miring penutup, yang menunjukkan benderaTata bahasa leksikal sangat lunak. tidak semua literal ekspresi reguler yang diidentifikasi sebagai satu token adalah ekspresi reguler yang valid
Lihat juga
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_78 untuk informasi lebih lanjutfunction comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_8Literal ekspresi reguler tidak dapat dimulai dengan dua garis miring (
function comment() {
/* console.log("Hello world!"); */
}
comment();
6), karena itu akan menjadi komentar baris. Untuk menentukan ekspresi reguler kosong, gunakan ________51______80Satu literal template terdiri dari beberapa token.
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
81 (kepala templat), function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
82 (templat tengah), dan function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
83(ekor templat) adalah token individual, sementara ekspresi apa pun dapat muncul di antaranyaLihat juga templat literal untuk informasi lebih lanjut
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_9Beberapa definisi sintaks pernyataan JavaScript memerlukan titik koma (
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
84) di bagian akhir. Mereka termasukfunction comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
00, const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
6, const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
7- Pernyataan ekspresi
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
88const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
4, const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
9, const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
2, const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
6const 你好 = "Hello";
console.log(\u4f60\u597d); // Hello
_5- Deklarasi bidang kelas (publik atau pribadi)
function import() {} // Illegal: import is a reserved word.
7, function import() {} // Illegal: import is a reserved word.
0
Namun, untuk membuat bahasa lebih mudah didekati dan nyaman, JavaScript dapat menyisipkan titik koma secara otomatis saat menggunakan aliran token, sehingga beberapa urutan token yang tidak valid dapat "diperbaiki" ke sintaks yang valid. Langkah ini terjadi setelah teks program diurai menjadi token sesuai dengan tata bahasa leksikal. Ada tiga kasus ketika titik koma disisipkan secara otomatis
1. Ketika token yang tidak diizinkan oleh tata bahasa ditemukan, dan dipisahkan dari token sebelumnya oleh setidaknya satu (termasuk komentar blok yang menyertakan setidaknya satu terminator baris), atau tokennya adalah "}", maka titik koma disisipkan sebelum
function comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
0Akhiran ")" dari
function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_88 diperlakukan sebagai kasus khusus oleh aturan ini jugafunction comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_1Namun, titik koma tidak disisipkan jika titik koma kemudian menjadi pemisah di kepala pernyataan
function import() {} // Illegal: import is a reserved word.
4function comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_2Titik koma juga tidak pernah dimasukkan sebagai pernyataan kosong. Misalnya, dalam kode di bawah ini, jika titik koma disisipkan setelah ")", maka kode tersebut akan valid, dengan pernyataan kosong sebagai badan
function import() {} // Illegal: import is a reserved word.
6 dan deklarasi const decl = 1; // Variable declaration (may also be `let` or `var`)
function fn() {} // Function declaration
const obj = { key: "value" }; // Object keys
// Class declaration
class C {
#priv = "value"; // Private property
}
lbl: console.log(1); // Label
7 sebagai pernyataan terpisah. Namun, karena titik koma yang disisipkan secara otomatis tidak dapat menjadi pernyataan kosong, ini menyebabkan a menjadi isi dari pernyataan function import() {} // Illegal: import is a reserved word.
6, yang tidak validfunction comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_32. Ketika akhir aliran input token tercapai, dan parser tidak dapat mengurai aliran input tunggal sebagai program lengkap, titik koma disisipkan di bagian akhir.
function comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_4Aturan ini merupakan pelengkap aturan sebelumnya, khususnya untuk kasus di mana tidak ada "token yang menyinggung" tetapi akhir aliran input
3. Ketika tata bahasa melarang terminator garis di suatu tempat tetapi terminator garis ditemukan, titik koma disisipkan. Tempat-tempat tersebut antara lain
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_01, function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
02function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_03function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_04function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_05function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_06function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_07function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_08function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_09function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
10, function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
11, function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
12, function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
13, function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
14
Di sini
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_15 tidak diperlakukan sebagai operator postfix yang menerapkan variabel function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
49, karena terminator garis terjadi antara function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
49 dan function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
15function comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
5Di sini, pernyataan
const obj = { import: "value" }; // Legal despite `import` being reserved
class C {
#import = "value";
}
2 mengembalikan function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
20, dan function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
21 menjadi pernyataan yang tidak dapat dijangkaufunction comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_6Perhatikan bahwa ASI hanya akan dipicu jika jeda baris memisahkan token yang akan menghasilkan sintaks yang tidak valid. Jika token berikutnya dapat diuraikan sebagai bagian dari struktur yang valid, titik koma tidak akan disisipkan. Sebagai contoh
function comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_7Karena
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_22 dapat dilihat sebagai pemanggilan fungsi, biasanya tidak akan memicu ASI. Demikian pula, function comment() {
/* This is a one line JavaScript comment */
console.log("Hello world!");
}
comment();
_77 dapat menjadi akses anggota. Kode di atas setara denganfunction comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_8Ini adalah sintaks yang valid.
function comment() {
/* This comment spans multiple lines. Notice
that we don't need to end the comment until we're done. */
console.log("Hello world!");
}
comment();
_24 adalah pengakses properti dengan ekspresi yang bergabung dengan koma. Oleh karena itu, Anda akan mendapatkan kesalahan seperti "1 bukan fungsi" dan "Tidak dapat membaca properti yang tidak terdefinisi (membaca 'forEach')" saat menjalankan kodeDi dalam kelas, bidang kelas dan metode generator juga bisa menjadi jebakan
function comment(x) {
console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");
_9Hal ini terlihat sebagai
function comment() {
/* console.log("Hello world!"); */
}
comment();
0Dan karena itu akan menjadi kesalahan sintaks sekitar ________52______25
Ada aturan praktis berikut untuk menangani ASI, jika Anda ingin menerapkan gaya tanpa titik koma
Apa yang bukan kata yang dilindungi undang-undang dalam JavaScript?
Identifier adalah IdentifierName yang bukan ReservedWord. Spesifikasi menjelaskan empat kelompok kata yang dipesan. kata kunci, kata cadangan masa depan, literal nol, dan literal boolean.
Kata kunci mana yang tidak kompatibel di ES6?
Apa kata-kata yang dicadangkan dalam JavaScript?
Kata yang dicadangkan .
merusak
menangkap
kelas
melanjutkan
debugger
bawaan
menghapus
Manakah dari berikut ini yang dicadangkan JavaScript?
Berikut adalah daftar kata-kata yang dicadangkan (dan kata-kata yang harus dihindari) dalam JavaScript. .
abstrak. boolean. merusak. byte. kasus. menangkap. arang. kelas. melanjutkan. const. debugger. bawaan. menghapus. .
terakhir. akhirnya. untuk. pergi ke. jika. alat. impor. di dalam. contoh. int. antarmuka. panjang. .
publik. kembali. pendek. statis. super. mengalihkan. disinkronkan. ini. melemparkan. melempar. sementara. BENAR