-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartsWith.js
More file actions
executable file
·49 lines (41 loc) · 1.07 KB
/
startsWith.js
File metadata and controls
executable file
·49 lines (41 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 5. Check if a String Starts With Another String
// 1. Using String.startsWith() Method
{
let s = "Hello WOrld";
let pre = "Hello";
if (s.startsWith(pre)) {
console.log(`The string starts with '${pre}'`);
} else {
console.log(`The string does not starts with '${pre}'`);
}
}
// 2. Using String.slice() Method
{
let s = "Hello world";
let prefix = "Hello";
if (s.slice(0, prefix.length) === prefix) {
console.log(`The string starts with '${prefix}'`);
} else {
console.log(`The string does not start with '${prefix}'`);
}
}
// 3. Using String.indexOf() Method
{
let s = "Hello World";
let prefix = "Hello";
if (s.indexOf(prefix) === 0) {
console.log(`The string starts with "${prefix}"`);
} else {
console.log(`The string does not start with '${prefix}'`);
}
}
// 4. Using String.substr() Method
{
let s = "Hello World";
let prefix = "Hello";
if (s.substr(0, prefix.length) === prefix) {
console.log(`The string starts with "${prefix}"`);
} else {
console.log(`The string does not start with '${prefix}'`);
}
}