You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71Lines changed: 71 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -65,6 +65,77 @@ const null_var = null;
65
65
console.log(null_var); // prints null to the console
66
66
```
67
67
68
+
### Strings
69
+
70
+
Some of the default string functions and properties are:
71
+
72
+
```javascript
73
+
conststr="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
+
conststr="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
+
conststr="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
+
constname="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
+
constnum=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
+
constnum=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
+
conststr="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
+
conststr="100px";
135
+
console.log(Number.parseInt(str, 10)); // 100
136
+
// It ignores the px at the end
137
+
```
138
+
68
139
Now, let's come to the most important part of dealing with variables, which is operations on them.
0 commit comments