Skip to content

Commit 760a1a6

Browse files
committed
added some files
1 parent 901ac78 commit 760a1a6

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

README.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ console.log(a % b); // prints 10 to the console
184184
There is also a shorthand notation of the operations, which is used to perform operations on the same variable.
185185

186186
```javascript
187-
var a = 10;
187+
let a = 10;
188188
a += 10; // a = a + 10
189189
console.log(a); // prints 20 to the console
190190
// Similarly, there are other operations like -=, *=, /=, %=
@@ -226,13 +226,13 @@ arr[2] = 10;
226226
console.log(arr); // prints [undefined, undefined, 10] to the console
227227
```
228228

229-
If a empty array is defined, then the array is filled with `undefined` values.
229+
If an empty array is defined, then the array is filled with `undefined` values.
230230

231231
This datatype also has some special properties, we can also apply stack and queue operations on it.
232232

233233
**Stack Operations**
234234
```javascript
235-
var arr = [1, 2, 3, 4, 5];
235+
let arr = [1, 2, 3, 4, 5];
236236
arr.push(6); // push operation
237237
console.log(arr); // prints [1, 2, 3, 4, 5, 6] to the console
238238
arr.pop(); // pop operation
@@ -244,30 +244,58 @@ When running a push operation, it will return the length of the array.
244244

245245
**Queue Operations**
246246
```javascript
247-
var arr = [1, 2, 3, 4, 5];
247+
let arr = [1, 2, 3, 4, 5];
248248
console.log(arr.shift()); // dequeue operation (prints 1 to the console)
249249
arr.unshift(6); // enqueue operation(to the first index)
250250
console.log(arr); // prints [6, 2, 3, 4, 5] to the console
251251
```
252252

253253
**Slicing an Array**
254254
```javascript
255-
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
256-
var slice = arr.slice(2, 5); // slice(start_index, count)
255+
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
256+
let slice = arr.slice(2, 5); // slice(start_index, count)
257257
console.log(slice); // prints [3, 4, 5, 6, 7] to the console
258258
console.log(arr); // prints [1, 2, 8, 9]
259259
```
260260

261261
To apply a specific operation on each element of the array, we can use the `forEach()` method.
262262

263263
```javascript
264-
var arr = [1, 2, 3, 4, 5];
264+
let arr = [1, 2, 3, 4, 5];
265265

266266
arr.forEach((element) => {
267267
console.log(element);
268268
});
269269
```
270270

271+
Filtering on the array can be done, using the `filter()` method. It will return a new array, which contains the elements which satisfy the condition.
272+
273+
```javascript
274+
let arr = [11, 25, 13, 42, 54, 6, 78];
275+
276+
let filtered = arr.filter((element) => {
277+
return element > 25;
278+
});
279+
console.log(filtered); // prints [42, 54, 78] to the console
280+
// Here you can see that only elements greater than 25 are returned
281+
```
282+
283+
The next method, which we are going to learn is the find method. It will return the first element which satisfies the condition. If no element is matched then, it will return `undefined`.
284+
285+
```javascript
286+
let arr = [11, 25, 13, 42, 54, 6, 78];
287+
288+
let filtered = arr.find((element) => {
289+
return element > 25;
290+
});
291+
console.log(filtered); // prints 42 to the console
292+
```
293+
294+
The main, difference between the `filter()` and `find`() methods is the return datatype of the methods, `filter()` returns an array, whereas `find()` returns the element.
295+
296+
* Filter will always return an array, even if there is only one element or no element in the array.
297+
* Find will return the element, if there is only one element in the array, otherwise it will return undefined.
298+
271299
## Conditions
272300

273301
Conditions are used to check whether a condition is true or false, and based on that we can perform some operations.

print.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello World");

variables.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const integer = 10; // Integer variable
2+
const string = "Hello World"; // String variable
3+
const float = 10.5; // Float variable
4+
const boolean = true; // Boolean variable
5+
6+
const arr = [1, 2, 3, 4, 5]; // Array variable
7+
const obj = { name: "John", age: 20 }; // Object variable (similar to dict in python)
8+
9+
let nd_var;
10+
console.log(nd_var); // prints undefined to the console
11+
12+
const null_var = null;
13+
console.log(null_var);
14+
15+
const const_var = 10;
16+
let let_var = 10;
17+
18+
const_var = 20; // throws an error in the JavaScript
19+
let_var = 20; // works fine
20+
21+
const str = "Hello World";
22+
console.log(str.length); // prints 11 to the console
23+
console.log(str.toUpperCase()); // prints HELLO WORLD to the console
24+
console.log(str.toLowerCase()); // prints hello world to the console
25+
26+
const str = "Hello World";
27+
console.log(str[0]); // prints H to the console
28+
console.log(str.at(0)); // prints H to the console
29+
console.log(str.at(-1)); // prints d to the console
30+
31+
const str = "Hello World";
32+
console.log(str.substring(0, 5)); // prints Hello to the console
33+
console.log(str.substring(6)); // prints World to the console
34+
35+
const name = "John";
36+
console.log(`Hello ${name}`); // prints Hello John to the console
37+
38+
const num = 10000;
39+
console.log(num.toString()); // prints 10000 to the console
40+
41+
const num = 100_000_000;
42+
// is same as 100000000
43+
44+
const str = "100";
45+
console.log(Number.parseInt(str, 10)); // 100, the second argument is the base of the number
46+
47+
const str = "100px";
48+
console.log(Number.parseInt(str, 10)); // 100
49+
// It ignores the px at the end

0 commit comments

Comments
 (0)