Skip to content

Commit 2b06e97

Browse files
committedDec 19, 2019
add string
1 parent 7432aeb commit 2b06e97

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
 

‎06-types-string.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
### String
2+
3+
- used for holding text
4+
5+
- 3 ways to create strings:
6+
7+
1. using single quotes:
8+
9+
`const first = 'Soumya';`
10+
11+
2. using double quotes:
12+
13+
`const middle = "Ranjan";`
14+
15+
3. using backticks:
16+
17+
```javascript
18+
const last = `Mohanty`;
19+
20+
```
21+
22+
- single quotes and double quotes are the same thing.
23+
24+
used for: `"she's cool"`
25+
26+
or escaping: `'she\\'s cool'`
27+
28+
- backticks:
29+
30+
```javascript
31+
const sentence = `she's so "cool"`;
32+
console.log(sentence); // she's so "cool"
33+
34+
```
35+
36+
- Multi-line string:
37+
38+
```javascript
39+
const song = 'Oh \\
40+
I like \\
41+
pizza';
42+
43+
console.log(song); // Oh I like pizza
44+
45+
```
46+
47+
```javascript
48+
const song = `Oh
49+
I like
50+
pizza`;
51+
52+
console.log(song);
53+
/*
54+
Oh
55+
I like
56+
pizza
57+
*/
58+
59+
```
60+
61+
2nd one using backticks is mostly used.
62+
63+
- **String concatenation and interpolation**
64+
65+
- '+' is used for **concatenation**. It is also used for adding 2 nos.
66+
- **Concatenation**: when 2 or more strings combined to one
67+
- **Interpolation**: when you put a variable inside a string
68+
- Example 1:
69+
70+
`const name = 'Soumya';`
71+
72+
`const hello = 'Hello my name is ' + name + '. Nice to meet you.'`
73+
74+
_(can use double or single quotes)_
75+
76+
- Example 2:
77+
78+
` 1+1 // 2`
79+
80+
`'1'+'1' // 11 `
81+
82+
`1 + '1' // 11`
83+
84+
- Example 3:
85+
86+
const name = 'Soumya'; const hello = `Hello my name is ${name}. Nice to meet you. I am ${100+1} years old.`; console.log(hello); // Hello my name is Soumya. Nice to meet you. I am 101 years old.
87+
88+
- Backticks also used for _tagged template literals_.
89+
90+
- Backticks are helpful for creating HTML:
91+
92+
```javascript
93+
const html = `
94+
<div>
95+
<h2>Hey everyone! I am ${name}.</h2>
96+
</div>
97+
`;
98+
```

0 commit comments

Comments
 (0)
Please sign in to comment.