Strings · String
Strings are text
- A string is text inside quotes:
"Ada"or'Ada'. - Join strings with
+— this is called concatenation.
String adalah teks
- String adalah teks di dalam tanda kutip:
"Ada"atau'Ada'. - Gabungkan string dengan
+— ini disebut konkatenasi.
let first = "Ada";
console.log("Hi, " + first + "!"); // → Hi, Ada!
Mind the spaces
+joins text exactly. It never adds a space for you.- Put the space inside the quotes, where you want it to appear.
Perhatikan spasi
+menggabungkan teks persis. Ia tidak pernah menambahkan spasi untuk Anda.- Letakkan spasi di dalam tanda kutip, tepat di mana Anda inginnya muncul.
let pet = "Rex";
console.log("Good dog," + pet + "!"); // → Good dog,Rex! (no space)
console.log("Good dog, " + pet + "!"); // → Good dog, Rex!
Length and capitals
.lengthcounts the characters..toUpperCase()and.toLowerCase()return a copy in CAPITALS or small letters.
Panjang dan huruf besar/kecil
.lengthmenghitung jumlah karakter..toUpperCase()dan.toLowerCase()mengembalikan salinan dalam HURUF BESAR atau huruf kecil.
console.log("hello".length); // → 5
console.log("hello".toUpperCase()); // → HELLO
Index and slice a string · Indeks dan iris string
Characters are numbered from 0. A slice takes from start up to — but not including — end. · Karakter dinomori mulai dari 0. Slice mengambil dari start hingga — tetapi tidak termasuk — end.
Reach inside a string
- Each character has a position, counting from 0:
text[0]is the first one. .slice(start, end)takes the part fromstartup to — but not including —end.
Akses isi string
- Setiap karakter memiliki posisi, dihitung dari 0:
text[0]adalah yang pertama. .slice(start, end)mengambil bagian daristarthingga — tetapi tidak termasuk —end.
let word = "JAVASCRIPT";
console.log(word[0]); // → J
console.log(word.slice(0, 4)); // → JAVA
Watch out
- Positions start at
0, so the 4th character isword[3]. - String methods return a new string — the original never changes.
Hati-hati
- Posisi dimulai dari
0, sehingga karakter ke-4 adalahword[3]. - Metode string mengembalikan string baru — yang asli tidak pernah berubah.
Common mistakes
- Index from
0;s.lengthis a property (no()). - Strings cannot be changed in place.
Kesalahan umum
- Indeks dari
0;s.lengthadalah properti (tanpa()). - String tidak dapat diubah secara in-place.
Now you try
- Build and inspect strings.
- Press Check answer to test it.
Sekarang Anda coba
- Bangun dan periksa string.
- Tekan Cek jawaban untuk mengujinya.
let name = "Sam". Use + to log Hello, then the name then !, so it reads Hello, Sam!. · let name = "Sam". Gunakan + untuk log Hello, lalu nama lalu !, sehingga terbaca Hello, Sam!.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Log how many characters are in the string computer using .length (it is 8). · Log berapa banyak karakter dalam string computer menggunakan .length (yaitu 8).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Log the string cat in CAPITALS using .toUpperCase() (it is CAT). · Log string cat dalam HURUF BESAR menggunakan .toUpperCase() (yaitu CAT).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
let word = "code". Log just its first letter using word[0] (it is c). · let word = "code". Catat hanya huruf pertamanya menggunakan word[0] (iaitu c).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.