You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/03-string/article.md
+36-35Lines changed: 36 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -310,13 +310,13 @@ if (str.indexOf("Widget") != -1) {
310
310
}
311
311
```
312
312
313
-
#### The bitwise NOT trick
313
+
#### Trik bitwise NOT
314
314
315
-
One of the old tricks used here is the[bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT)`~` operator. It converts the number to a 32-bit integer (removes the decimal part if exists) and then reverses all bits in its binary representation.
315
+
Salah satu trik lama yang digunakan disini adalah operator[bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT)`~`. Operator ini mengubah angka menjadi integer 32-bit (menghilangkan bagian desimal jika ada) lalu menegasikan semua bit pada representasi binernya.
316
316
317
-
In practice, that means a simple thing: for 32-bit integers `~n`equals`-(n+1)`.
317
+
Dalam praktik, hal tersebut berarti: untuk integer 32-bit `~n`sama dengan`-(n+1)`.
318
318
319
-
For instance:
319
+
Sebagai contoh:
320
320
321
321
```js run
322
322
alert( ~2 ); // -3, the same as -(2+1)
@@ -327,11 +327,11 @@ alert( ~-1 ); // 0, the same as -(-1+1)
327
327
*/!*
328
328
```
329
329
330
-
As we can see, `~n`is zero only if `n == -1` (that's for any 32-bit signed integer `n`).
330
+
Seperti yang kita lihat, `~n`bernilai nol apabila `n == -1` (untuk semua signed integer `n`).
331
331
332
-
So, the test `if ( ~str.indexOf("...") )`is truthy only if the result of `indexOf`is not`-1`. In other words, when there is a match.
332
+
Jadi, pengecekan di dalam `if ( ~str.indexOf("...") )`bernilai benar apabila hasil dari `indexOf`tidak bernilai`-1`. Dengan kata lain, jika kemunculan ditemukan.
333
333
334
-
People use it to shorten`indexOf` checks:
334
+
Orang-orang menggunakannya untuk memperpendek pengecekan`indexOf`:
335
335
336
336
```js run
337
337
let str ="Widget";
@@ -341,63 +341,63 @@ if (~str.indexOf("Widget")) {
341
341
}
342
342
```
343
343
344
-
It is usually not recommended to use language features in a non-obvious way, but this particular trick is widely used in old code, so we should understand it.
344
+
Biasanya tidak direkomendasikan untuk menggunakan fitur bahasa dengan cara yang tidak jelas, tetapi trik ini biasa digunakan di kode yang kuno, jadi kita harus memahaminya.
345
345
346
-
Just remember: `if (~str.indexOf(...))`reads as "if found".
346
+
Ingat: `if (~str.indexOf(...))`dibaca sebagai "if ditemukan".
347
347
348
-
To be precise though, as big numbers are truncated to 32 bits by`~` operator, there exist other numbers that give`0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long.
348
+
Untuk lebih detail, karena bilangan yang besar dipotong menjadi 32-bit oleh operator`~`, ada angka lain yang memberikan hasil`0`, angka yang terkecil yaitu `~4294967295=0`. Hal tersebut menyebabkan trik ini benar apabila string yang dites tidak sepanjang itu.
349
349
350
-
Right now we can see this trick only in the old code, as modern JavaScript provides`.includes`method (see below).
350
+
Kita dapat melihat bahwa trik ini hanya digunakan di kode yang kuno, karena Javascript modern menyediakan method`.includes`(lihat di bawah).
351
351
352
352
### includes, startsWith, endsWith
353
353
354
-
The more modern method [str.includes(substr, pos)](mdn:js/String/includes)returns`true/false`depending on whether`str`contains`substr`within.
354
+
Method yang lebih modern [str.includes(substr, pos)](mdn:js/String/includes)mengembalikan`true/false`tergantung dengan apakah`str`mengandung`substr`di dalamnya.
355
355
356
-
It's the right choice if we need to test for the match, but don't need its position:
356
+
Ini adalah pilihan yang cocok apabila kita hanya perlu mengetes apakah `substr` ada, tetapi tidak memerlukan posisinya:
357
357
358
358
```js run
359
359
alert( "Widget with id".includes("Widget") ); // true
360
360
361
361
alert( "Hello".includes("Bye") ); // false
362
362
```
363
363
364
-
The optional second argument of `str.includes`is the position to start searching from:
364
+
Parameter opsinal kedua dari `str.includes`adalah posisi dimana pencarian mulai dilakukan:
365
365
366
366
```js run
367
367
alert( "Widget".includes("id") ); // true
368
368
alert( "Widget".includes("id", 3) ); // false, from position 3 there is no "id"
369
369
```
370
370
371
-
The methods [str.startsWith](mdn:js/String/startsWith)and[str.endsWith](mdn:js/String/endsWith)do exactly what they say:
371
+
Method [str.startsWith](mdn:js/String/startsWith)dan[str.endsWith](mdn:js/String/endsWith)melakukan fungsi seperti namanya:
372
372
373
373
```js run
374
374
alert( "Widget".startsWith("Wid") ); // true, "Widget" starts with "Wid"
375
375
alert( "Widget".endsWith("get") ); // true, "Widget" ends with "get"
376
376
```
377
377
378
-
## Getting a substring
378
+
## Mengambil substring
379
379
380
-
There are 3 methods in JavaScript to get a substring: `substring`, `substr`and`slice`.
380
+
Ada 3 cara untuk mengambil sebuah substring di Javascript: `substring`, `substr`dan`slice`.
381
381
382
382
`str.slice(start [, end])`
383
-
: Returns the part of the string from`start`to (but not including) `end`.
383
+
: Mengembalikan bagian dari string dari`start`sampai (tapi tidak termasuk) `end`.
384
384
385
-
For instance:
385
+
Sebagai contoh:
386
386
387
387
```js run
388
388
let str = "stringify";
389
389
alert( str.slice(0, 5) ); // 'strin', the substring from 0 to 5 (not including 5)
390
390
alert( str.slice(0, 1) ); // 's', from 0 to 1, but not including 1, so only character at 0
391
391
```
392
392
393
-
If there is no second argument, then `slice` goes till the end of the string:
393
+
Jika tidak ada parameter kedua, maka `slice` akan mengambil semua bagian dari `start` sampai akhir string:
394
394
395
395
```js run
396
396
let str = "st*!*ringify*/!*";
397
397
alert( str.slice(2) ); // ringify, from the 2nd position till the end
398
398
```
399
399
400
-
Negative values for `start/end` are also possible. They mean the position is counted from the string end:
400
+
Nilai negatif untuk `start/end` juga bisa digunakan. Nilai negatif berarti posisinya dihitung dari akhir string:
401
401
402
402
```js run
403
403
let str = "strin*!*gif*/!*y";
@@ -407,11 +407,11 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
407
407
```
408
408
409
409
`str.substring(start [, end])`
410
-
: Returns the part of the string *between*`start`and`end`.
410
+
: Mengembalikan bagian dari string *di antara*`start`dan`end`.
411
411
412
-
This is almost the same as `slice`, but it allows `start` to be greater than `end`.
412
+
Method ini hampir sama dengan `slice`, tetapi nilai `start` boleh lebih besar daripada `end`.
413
413
414
-
For instance:
414
+
Sebagai contoh:
415
415
416
416
```js run
417
417
let str = "st*!*ring*/!*ify";
@@ -426,37 +426,38 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
426
426
427
427
```
428
428
429
-
Negative arguments are (unlike slice) not supported, they are treated as `0`.
429
+
Parameter negatif tidak didukung (tidak seperti slice), mereka dianggap sebagai `0`.
430
430
431
431
`str.substr(start [, length])`
432
-
: Returns the part of the string from `start`, with the given`length`.
432
+
: Mengembalikan substring dari `start`, dengan panjang`length`.
433
433
434
-
In contrast with the previous methods, this one allows us to specify the `length` instead of the ending position:
434
+
Dibandingkan dengan cara-cara sebelumnya, cara ini memperbolehkan kita untuk menyebutkan `length` alih-alih posisi akhir dari string:
435
435
436
436
```js run
437
437
let str = "st*!*ring*/!*ify";
438
438
alert( str.substr(2, 4) ); // ring, from the 2nd position get 4 characters
439
439
```
440
440
441
-
The first argument may be negative, to count from the end:
441
+
Parameter pertama mungkin bernilai negatif, untuk menghitung dari akhir string:
442
442
443
443
```js run
444
444
let str = "strin*!*gi*/!*fy";
445
445
alert( str.substr(-4, 2) ); // gi, from the 4th position get 2 characters
446
446
```
447
447
448
-
Let's recap these methods to avoid any confusion:
448
+
Mari kita review cara-cara tersebut untuk menghindari kebingungan:
```smart header="Cara mana yang harus kita gunakan?"
457
457
All of them can do the job. Formally, `substr` has a minor drawback: it is described not in the core JavaScript specification, but in Annex B, which covers browser-only features that exist mainly for historical reasons. So, non-browser environments may fail to support it. But in practice it works everywhere.
458
+
Semuanya dapat melakukan pekerjaannya. Secara formal, `substr` memiliki kekurangan: fungsi ini tidak tercantum di spesifikasi inti Javascript, tetapi di Annex B, yang mencakup hanya fitur browser yang ada karena alasan historis. Jadi, environment non-browser mungkin gagal untuk mendukungnya. Tetapi dalam praktik fungsi ini bekerja di mana saja.
458
459
459
-
Of the other two variants, `slice` is a little bit more flexible, it allows negative arguments and shorter to write. So, it's enough to remember solely `slice` of these three methods.
460
+
Dibandingkan dengan dua varian yang lain, `slice` lebih fleksibel, karena memperbolehkan parameter negatif dan lebih pendek untuk ditulis. Jadi, dari ketiga cara sudah cukup untuk mengingat `slice`.
0 commit comments