Skip to content

Commit 537d6cb

Browse files
committed
added readme file
1 parent 57d05ef commit 537d6cb

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,77 @@ const null_var = null;
6565
console.log(null_var); // prints null to the console
6666
```
6767

68+
### Strings
69+
70+
Some of the default string functions and properties are:
71+
72+
```javascript
73+
const str = "Hello World";
74+
console.log(str.length); // prints 11 to the console
75+
console.log(str.toUpperCase()); // prints HELLO WORLD to the console
76+
console.log(str.toLowerCase()); // prints hello world to the console
77+
```
78+
79+
Individual characters in the strings can be accessed by knowing the index, using two methods square bracket notation and `.at()` method.
80+
81+
One thing to note is that the index of the string starts from 0. One advantage of using `.at()` method is that we can use negative indices to access the characters from the end of the string.
82+
83+
```javascript
84+
const str = "Hello World";
85+
console.log(str[0]); // prints H to the console
86+
console.log(str.at(0)); // prints H to the console
87+
console.log(str.at(-1)); // prints d to the console
88+
```
89+
90+
Getting a part of the strings is one of the useful things in programming, which can be done by using the `.substring()` method.
91+
92+
```javascript
93+
const str = "Hello World";
94+
console.log(str.substring(0, 5)); // prints Hello to the console
95+
console.log(str.substring(6)); // prints World to the console
96+
```
97+
98+
The second argument is always optional, if you don't specify it, it will assume that the second argument is the length of the string.
99+
100+
After this, another useful thing is to know about the template literals, which are used to print the variables in the strings. The template strings are enclosed within the backtick symbol "`".
101+
102+
```javascript
103+
const name = "John";
104+
console.log(`Hello ${name}`); // prints Hello John to the console
105+
```
106+
107+
### Numbers
108+
109+
Numbers are one of the most important things in programming, and JS has a lot of functions and properties to deal with numbers.
110+
111+
```javascript
112+
const num = 10000;
113+
114+
console.log(num.toString()); // prints 10000 to the console
115+
```
116+
117+
Large and Long numbers can be represented with `_` using the underscore notation, to avoid confusion in reading them.
118+
119+
```javascript
120+
const num = 100_000_000;
121+
// is same as 100000000
122+
```
123+
124+
When you want to convert a string into a number, you can use `parseInt()` method.
125+
126+
```javascript
127+
const str = "100";
128+
console.log(Number.parseInt(str, 10)); // 100, the second argument is the base of the number
129+
```
130+
131+
There is also one usage of the above function. When the string starts with a number and ends with a non-numeric character, still the `parseInt()` function will return the number.
132+
133+
```javascript
134+
const str = "100px";
135+
console.log(Number.parseInt(str, 10)); // 100
136+
// It ignores the px at the end
137+
```
138+
68139
Now, let's come to the most important part of dealing with variables, which is operations on them.
69140

70141
## Operations

0 commit comments

Comments
 (0)