-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
52 lines (35 loc) · 1.63 KB
/
main.js
File metadata and controls
52 lines (35 loc) · 1.63 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
50
// STRING METHODS
//1.toUpperCase()- Converts all characters or string to uppercase.
let myName = "david"
let uppercaseName = myName.toUpperCase()
console.log(uppercaseName)//DAVID
//2.toLowerCase()- converts all characters or string to lowercase.
let name = "DAVID"
let lowercaseName = myName.toLowerCase()
console.log(lowercaseName)//david
//3.trim()-Removes whitespace (spaces, tabs, newlines) from the beginning and end of a string.
// trimStart() → removes only from the beginning.
// trimEnd() → removes only from the end.
let language = " JavaScript "
let trimmedLanguage = language.trim()
console.log(trimmedLanguage)//JavaScript with no space
//4.split()-Splits a string into an array, based on a specified separator.
let fruitsText = "apple,mango,passion,banana"
let splitText = fruitsText.split(",")
console.log(splitText)//[ 'apple', 'mango', 'passion', 'banana' ]
//if no operator given
let word = "hello"
let splitWord = word.split("")
console.log(splitWord)//[ 'h', 'e', 'l', 'l', 'o' ]
//5.replace()-Replaces a specified value with another in a string
let str1 = "I love swimming"
let str2 = str1.replace("swimming", "JavaScript")
console.log(str2)//I love JavaScript
//To replace all occurrences, use a regular expression with the g flag:
let text = "JS is fun. JS is powerful.";
let result = text.replace(/JS/g, "JavaScript");
console.log(result); // "JavaScript is fun. JavaScript is powerful."
//6.includes()-Checks if a string contains a certain substring. Returns true or false.
let text2 = "Learning JavaScript is fun!";
console.log(text.includes("JavaScript")); // true
console.log(text.includes("Python")); // false