-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtut6.js
More file actions
66 lines (45 loc) · 1.91 KB
/
tut6.js
File metadata and controls
66 lines (45 loc) · 1.91 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Strings: Properties, Methods & Template Literals in JavaScript
// concat
const fname = "ram";
const lname = "prasad";
name = fname+lname; // method 1
console.log(name);
name = fname.concat(lname); // method 2
console.log(name);
// length
console.log(name.length);
// toLowerCase and toUpperCase
console.log(name.toUpperCase());
console.log(name.toLowerCase());
// slicing string
console.log(name[0]);
console.log(name[1]);
// slicing string using charAt
console.log(name.charAt(0));
console.log(name.charAt(1));
// find index of charector using
console.log(name.indexOf('a')); // it will print 1st index of char
console.log(name.lastIndexOf('a')); // it will print last index of char
console.log(name.indexOf('z')); // it will print -1 because char 'z' not exists in string
// endsWith
console.log(name.endsWith('d')); // it will print true
// includes
console.log(name.includes('y')); // it is just like contains it will print false
// substring: it will slice string between given char position (only positive)
console.log(name.substring(0,2)); // print char between 0 to 2 (n to n-1)
console.log(name.substring(-3)); // it will return complete string
// slice: it will slice string between given char position (positive and negative both)
console.log(name.slice(0,2)); // print char between 0 to 2 (n to n-1)
console.log(name.slice(-3)); // it will return last 3 char
// split : it will split the string as per delimater and return array
let txt = "ram is a good boy. my god is ram";
console.log(txt.split(' '))
// replace : it will replace only 1st occurance
console.log(txt.replace('ram', 'shyam'))
// Template Literals
let fruit1 = 'Orange\'';
let fruit2 = 'Apple';
let myhtml = `Hello ${fname},
<h4> How are "you. </h4>
Do you like '${fruit1} and ${fruit2}?`
document.body.innerHTML = document.body.innerHTML.concat(myhtml);