Skip to content

Commit 7714913

Browse files
committed
07 Strings.JS
Learned about new syntax of concatinating strings, accessing methods of strings e.t.c.
1 parent cebddca commit 7714913

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

01_Basics/07_strings.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// 7. Strings
2+
3+
// (i) Concatenation in Strings,
4+
const name = "Ayush"
5+
const repoCount = 19
6+
// Outdated Syntax,
7+
console.log(name + repoCount + " etc."); // Output : Ayush19 etc.
8+
// Modern day Syntax,
9+
console.log(`My name is ${name} and I have total ${repoCount} repositories on GiHub.`);
10+
// Output : My name is Ayush and I have total 19 repositories on GiHub.
11+
// Note: Benifits of this syntax is that we can use methods like, ${name.toUpperCase} e.t.c.
12+
13+
// (ii) One more way of declaring strings,
14+
const gameName = new String('SuperMario') /* When we run this in Browser's console we can see
15+
some interesting things there.
16+
17+
Some Methods of String, */
18+
// (a) Accessing 0th key of gameName,
19+
console.log(gameName[0]); // Output : S
20+
// (b) Accessing prototype,
21+
console.log(gameName.__proto__); // Output : {}
22+
23+
// (c) Accessing methods of Prototype,
24+
console.log(gameName.length); // Output : 10
25+
console.log(gameName.toUpperCase()); // Output : SUPERMARIO
26+
// This will not change our original string cause as we know about Stack.
27+
console.log(gameName.charAt(3)); // Output : e
28+
console.log(gameName.indexOf('M')); // Output : 5
29+
30+
const discordName = "hitesh-hc"
31+
console.log(discordName.length); // Output : 9
32+
const newString = discordName.substring(0, 4) // It will not include the 4th one.
33+
console.log(newString); // Output : hite
34+
35+
const anotherString = discordName.slice(-8, 4)
36+
console.log(anotherString); /* Output : ite
37+
Note : We cannot give negative values in substring but we can give negative value in slice. */
38+
39+
const oneNewString = " hitesh "
40+
console.log(oneNewString.trim()) // Output : hitesh (will remove that spaces around the string.)
41+
42+
const url = "https://ayush.com/ayush%20yadav"
43+
console.log(url.replace('%20', '-')); /* Output : https://ayush.com/ayush-yadav
44+
Also, */
45+
console.log(url.includes('ayush')) // Output : true
46+
47+
console.log(discordName.split('-')); /* Output : [ 'hitesh', 'hc' ] (Converted into array with
48+
two strings) */
49+
50+
51+
52+

0 commit comments

Comments
 (0)