Skip to content

Commit 2b498e8

Browse files
committed
js explore
1 parent c7dcf1e commit 2b498e8

34 files changed

Lines changed: 546 additions & 223 deletions

1_part/conditions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// if-else
2+
3+
const isAdmin = true;
4+
5+
if(isAdmin){
6+
console.log("You are admin")
7+
}else{
8+
console.log("Only admin can access")
9+
}
10+

1_part/datatype.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
typeof undefined; // "undefined"
2+
3+
typeof 0; // "number"
4+
5+
typeof 10n; // "bigint"
6+
7+
typeof true; // "boolean"
8+
9+
typeof "foo"; // "string"
10+
11+
typeof Symbol("id"); // "symbol"
12+
13+
typeof Math; // "object" (1)
14+
15+
typeof null; // "object" (2)
16+
17+
typeof alert; // "function" (3)
18+
19+
console.log(typeof alert)

1_part/function.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// functions
2+
3+
function greet(name) {
4+
return `Hello, ${name}!`;
5+
}
6+
7+
console.log(greet("Amresh"));
8+
9+
// function expression
10+
const greet2 = function (name) {
11+
return `Hello, ${name}!`;
12+
};
13+
14+
console.log(greet2("Amresh")); // Outputs: Hello, Amresh!
15+
16+
// arrow functions
17+
const greet = (name) => `Hello, ${name}!`;
18+
19+
console.log(greet("Amresh")); // Outputs: Hello, Amresh!
20+
21+
// anonymous function
22+
setTimeout(function () {
23+
console.log("This will run after 2 seconds");
24+
}, 2000);
25+
26+
// IIFE
27+
(function () {
28+
console.log("This function runs immediately!");
29+
})();
30+
31+
// Constructor Function
32+
function Person(name, age) {
33+
this.name = name;
34+
this.age = age;
35+
}
36+
37+
const person1 = new Person("Amresh", 25);
38+
console.log(person1.name); // Outputs: Amresh
39+
40+
// Generator Function
41+
// Generator functions return an iterator object that can be used to control the execution of the function, allowing you to pause and resume execution.
42+
function* generateNumbers() {
43+
yield 1;
44+
yield 2;
45+
yield 3;
46+
}
47+
48+
const generator = generateNumbers();
49+
50+
console.log(generator.next().value); // Outputs: 1
51+
console.log(generator.next().value); // Outputs: 2
52+
console.log(generator.next().value); // Outputs: 3
53+
54+
// HOF(Higher Order Function)
55+
// A higher-order function is a function that takes another function as an argument or returns a function.
56+
57+
function higherOrder(fn) {
58+
return fn();
59+
}
60+
61+
const greet3 = () => "Hello!";
62+
63+
console.log(higherOrder(greet3)); // Outputs: Hello!
64+
65+
// Callback Function
66+
// A callback function is a function passed as an argument to another function and is executed after some operation has been completed.
67+
function processUserInput(callback) {
68+
const name = "Amresh";
69+
callback(name);
70+
}
71+
72+
processUserInput(function (name) {
73+
console.log(`Hello, ${name}!`); // Outputs: Hello, Amresh!
74+
});

1_part/input.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
let age = parseInt(prompt("enter age: "))
3+
4+
console.log("Your Age: ",age)

1_part/loops.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// loop
2+
3+
let arr = ["A","M","R","E","S","H"]
4+
5+
// for loop
6+
for (let index = 0; index < arr.length; index++) {
7+
console.log(arr[index])
8+
}
9+
let i = 0;
10+
while(i<arr.length){
11+
console.log(arr[i])
12+
i++;
13+
}
14+
15+
// do while
16+
let j = 0;
17+
do {
18+
console.log(j);
19+
j++;
20+
} while (j < 5);
21+
22+
23+
24+
// for in loop
25+
26+
// for (variable in object) {
27+
// // code to be executed
28+
// }
29+
30+
for(let char in arr){
31+
console.log(char)
32+
}
33+
34+
35+
// for of loop
36+
// for (variable of iterable) {
37+
// code to be executed
38+
// }
39+
const person = { name: "Amresh", age: 25, city: "Delhi" };
40+
41+
for (let key in person) {
42+
console.log(key + ": " + person[key]);
43+
// Outputs: name: Amresh, age: 25, city: Delhi
44+
}
45+
let str = "JavaScript";
46+
47+
for (let char of str) {
48+
console.log(char); // Outputs each character of the string: 'J', 'a', 'v', etc.
49+
}
50+
51+
52+
// method loop - forEach
53+
arr.forEach(function(element, index, array) {
54+
// code to be executed
55+
console.log(index+1,". ",element)
56+
});
57+
58+
59+
// method - map loop
60+
arr.map((item)=>{
61+
console.log(item)
62+
})

1_part/operator.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Operators
2+
3+
// AND ( & )
4+
// OR ( | )
5+
// XOR ( ^ )
6+
// NOT ( ~ )
7+
// LEFT SHIFT ( << )
8+
// RIGHT SHIFT ( >> )
9+
// ZERO-FILL RIGHT SHIFT ( >>> )
10+
11+
// typeof: Returns the type of a variable.
12+
// instanceof: Checks if an object is an instance of a class.
13+
14+
const arr = [1, 3, 4];
15+
console.log(arr instanceof Array); // true
16+
console.log("Hello" + 5); // hello5
17+
console.log(3 + 7); //10
18+
console.log(1 / 0); // Inifinty
19+
console.log(5 / 2); //2.5
20+
console.log(8 % 3); // 2
21+
console.log(1.2 == 1 + 0.2); //true
22+
console.log(1.2 === 1 + 0.2); //true
23+
console.log(0 != 0); //false
24+
console.log(-Infinity < Infinity); //true

1_part/string.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// string
2+
// 3 ways
3+
// 1. '' single quote
4+
// 2. "" double quote
5+
// 3. `` backticks
6+
7+
console.log("---- ---- ----");
8+
9+
const PORT = "8080";
10+
const DB = "MYSQL";
11+
const URI = `http://localhost:${PORT}`;
12+
13+
console.log(URI);
14+
15+
console.log("---- ---- ----");
16+
let name = "IllyaRaja";
17+
18+
console.log(`hello ${1}`); // 1
19+
20+
console.log(`hello ${"name"}`); // name
21+
22+
console.log(`hello ${name}`); // IllyaRaja
23+
console.log("---- ---- ----");
24+
25+

1_part/switch.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// switch
2+
3+
let char = prompt("enter char");
4+
5+
switch (char) {
6+
case "a" || "A":
7+
console.log("vowel " + char);
8+
break;
9+
case "e" || "E":
10+
console.log("vowel " + char);
11+
break;
12+
13+
case "i" || "I":
14+
console.log("vowel " + char);
15+
break;
16+
17+
case "o" || "O":
18+
console.log("vowel " + char);
19+
break;
20+
21+
case "u" || "U":
22+
console.log("vowel " + char);
23+
break;
24+
25+
default:
26+
console.log("consonent " + char);
27+
}

1_part/variable.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// variables
2+
// 3 keyword
3+
// 1.var (old way not recommended)
4+
// 2.let
5+
// 3.const
6+
7+
var planet ="earth"
8+
let name = "Amresh";
9+
const PI=3.14
10+
11+
console.log(planet)
12+
console.log(name)
13+
console.log(PI)
14+
15+
// let and const can't be declared twice.

2_part/arrays.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// arrays
2+
3+
const fruits = ["apple", "banana", "cherry"];
4+
const numbers = new Array(1, 2, 3);
5+
//empty array
6+
const emptyArray = new Array();
7+
const lengthArray = new Array(5); // Creates an empty array of length 5
8+
9+
console.log(fruits[0]); // Outputs: 'apple'
10+
console.log(fruits[2]); // Outputs: 'm
11+
12+
const moreFruits = ["pineapple", "berry"];
13+
const allFruits = fruits.concat(moreFruits);
14+
console.log(allFruits); // Outputs: ['kiwi', 'orange', 'mango', 'pineapple', 'berry']
15+
16+
fruits.forEach((fruit) => {
17+
console.log(fruit);
18+
});
19+
// Outputs: 'kiwi', 'lemon', 'orange', 'mango'
20+
21+
const shortFruits = fruits.filter((fruit) => fruit.length < 6);
22+
console.log(shortFruits); // Outputs: ['kiwi', 'lemon', 'mango']
23+
24+
const found = fruits.find((fruit) => fruit.startsWith("m"));
25+
console.log(found); // Outputs: 'mango'
26+
27+
const numbers2 = [1, 2, 3, 4];
28+
const sum = numbers2.reduce((total, num) => total + num, 0);
29+
console.log(sum); // Outputs: 10
30+
31+
fruits.sort();
32+
console.log(fruits); // Outputs: ['kiwi', 'lemon', 'mango', 'orange']
33+
34+
const numbers3 = [40, 100, 1, 5];
35+
numbers3.sort((a, b) => a - b);
36+
console.log(numbers3); // Outputs: [1, 5, 40, 100]
37+
38+

0 commit comments

Comments
 (0)