├── .gitignore ├── 1. Intro to JavaScript └── 1.js ├── 2. Basic of JavaScript ├── 1.js ├── 10.js ├── 11.js ├── 12.js ├── 2.js ├── 3.js ├── 4.js ├── 5.js ├── 6.js ├── 7.js ├── 8.js └── 9.js ├── 3. Control Flow of JavaScript ├── 1.js ├── 10.js ├── 11.js ├── 12.js ├── 13.js ├── 2.js ├── 3.js ├── 4.js ├── 5.js ├── 6.js ├── 7.js ├── 8.js └── 9.js ├── 4. Functions in JavaScript ├── 1.js ├── 10.js ├── 11.js ├── 12.js ├── 13.js ├── 14.js ├── 15.js ├── 16.js ├── 17.js ├── 18.js ├── 19.js ├── 2.js ├── 20.js ├── 3.js ├── 4.js ├── 5.js ├── 6.js ├── 7.js ├── 8.js ├── 9.js └── index.html ├── 5. Objects in JavaScript ├── 1.js ├── 10.js ├── 11.js ├── 12.js ├── 13.js ├── 14.js ├── 15.js ├── 16.js ├── 17.js ├── 18.js ├── 19.js ├── 2.js ├── 20.js ├── 21.js ├── 22.js ├── 23.js ├── 24.js ├── 25.js ├── 26.js ├── 27.js ├── 28.js ├── 3.js ├── 4.js ├── 5.js ├── 6.js ├── 7.js ├── 8.js └── 9.js ├── 6. Types in JavaScript ├── 1.js ├── 10.js ├── 11.js ├── 12.js ├── 13.js ├── 14.js ├── 15.js ├── 16.js ├── 17.js ├── 18.js ├── 19.js ├── 2.js ├── 20.js ├── 21.js ├── 22.js ├── 23.js ├── 24.js ├── 25.js ├── 26.js ├── 27.js ├── 28.js ├── 29.js ├── 3.js ├── 30.js ├── 31.js ├── 32.js ├── 33.js ├── 34.js ├── 35.js ├── 36.js ├── 37.js ├── 38.js ├── 39.js ├── 4.js ├── 40.js ├── 41.js ├── 42.js ├── 43.js ├── 44.js ├── 45.js ├── 46.js ├── 47.js ├── 48.js ├── 49.js ├── 5.js ├── 6.js ├── 7.js ├── 8.js ├── 9.js ├── package.json └── worksheet.js ├── 7. Array-Methods ├── 1. at().js ├── 10. findIndex().js ├── 11. findLastIndex().js ├── 12. flat().js ├── 13. flatMap().js ├── 14. forEach().js ├── 15. from().js ├── 16. includes().js ├── 17. indexof().js ├── 18. isArray().js ├── 19. join().js ├── 2. concat().js ├── 20. keys().js ├── 21. lastindexof().js ├── 22. map().js ├── 23. of().js ├── 24. pop().js ├── 25. push().js ├── 26. reduce().js ├── 27. reduceRight().js ├── 28. reverse().js ├── 29. shift().js ├── 3. constructor().js ├── 30. slice().js ├── 31. some().js ├── 32. sort().js ├── 33. splice().js ├── 34. toLocaleString().js ├── 35. toString().js ├── 36. unshift().js ├── 37. values().js ├── 38. length().js ├── 4. copyWithin().js ├── 5. entries().js ├── 6. every().js ├── 7. fill().js ├── 8. filter().js ├── 9. find().js └── all-methods.jpg ├── 8. String-Methods ├── 1. charAt.js ├── 10. length.js ├── 11. localeCompare().js ├── 12. match().js ├── 13. matchAll().js ├── 14. padEnd().js ├── 15. padStart().js ├── 16. repeat().js ├── 17. replace().js ├── 18. replaceAll().js ├── 19. search().js ├── 2. charCodeAt().js ├── 20. slice().js ├── 21. split().js ├── 22. startsWith().js ├── 23. substring().js ├── 24. toLowerCase().js ├── 25. toUpperCase().js ├── 26. trim().js ├── 3. codePointAt().js ├── 4. concat().js ├── 5. endsWith().js ├── 6. fromCharCode().js ├── 7. fromCodePoint().js ├── 8. includes().js ├── 9. indexof().js └── list.js ├── 9. Object-Methods ├── 1. Object.assign.js ├── 10. Object getOwnPropertyNames().js ├── 11. Object getOwnPropertySymbols().js ├── 12. Object.getPrototypeOf().js ├── 13. Object hasOwnProperty().js ├── 14. Object.is().js ├── 15. Object.isExtensible().js ├── 16. Object.isFrozen().js ├── 17. Object.isPrototypeOf().js ├── 18. Object.isSealed().js ├── 19. Object.keys().js ├── 2. Object.create.js ├── 20. Object.preventExtensions().js ├── 21. Object.propertyIsEnumerable().js ├── 22. Object.seal().js ├── 23. Object.setPrototypeOf().js ├── 24. Object toLocaleString().js ├── 25. Object toString().js ├── 26. Object valueOf().js ├── 27. Object.values().js ├── 3. Object.defineProperties().js ├── 4. Object.defineProperty().js ├── 5. Object.entries().js ├── 6. Object.freeze().js ├── 7. Object.fromEntries().js ├── 8. Object getOwnPropertyDescriptor().js ├── 9. Object getOwnPropertyDescriptors().js └── Important.txt ├── Advanced-JavaScript ├── 10. Function-Methods │ ├── 1. apply().js │ ├── 2. bind().js │ ├── 3. call().js │ ├── 4. name().js │ ├── 5. length.js │ └── 6. toString().js ├── 11. Number-Methods │ ├── 1. isFinite().js │ ├── 2. isInteger().js │ ├── 3. parseFloat().js │ ├── 4. parseInt().js │ ├── 5. isNan().js │ ├── 6. isSafeInteger().js │ ├── 7. toExponential().js │ ├── 8. toFixed().js │ ├── 9. toExponential().js │ └── Other.js ├── 12. Symbol in JavaScript │ ├── 1.js │ ├── 2.js │ ├── 3.js │ ├── 4.js │ ├── 5.js │ ├── 6.js │ ├── 7.js │ └── 8.js ├── 13. Exceptions and Modules │ ├── 1. Intro.js │ ├── 2. try...catch.js │ ├── 3. try..catch..finally.js │ ├── 4. other.js │ ├── 5 throw.js │ ├── 6. modules1.js │ ├── 7. modules2.js │ ├── 8. modules3.js │ ├── 9. modules4.js │ └── package.json ├── 14. ES6 │ ├── 1.js │ ├── 10.js │ ├── 11.js │ ├── 12.js │ ├── 13.js │ ├── 14.js │ ├── 15.js │ ├── 2.js │ ├── 3.js │ ├── 4.js │ ├── 5.js │ ├── 6.js │ ├── 7.js │ ├── 8.js │ ├── 9.js │ ├── package-lock.json │ └── package.json ├── 15. JavaScript Asynchronous │ ├── 1.js │ ├── 10.js │ ├── 11.js │ ├── 12.js │ ├── 2.js │ ├── 3.js │ ├── 4.js │ ├── 5.js │ ├── 6.js │ ├── 7.js │ ├── 8.js │ ├── 9.js │ ├── index.html │ ├── package-lock.json │ └── package.json └── 16. JavaScript Miscellaneous │ ├── 1.js │ ├── 1.json │ ├── 10.js │ ├── 11.js │ ├── 12.js │ ├── 13.js │ ├── 2.js │ ├── 2.jsonc │ ├── 3.js │ ├── 4.js │ ├── 5.js │ ├── 6.js │ ├── 7.js │ ├── 8.js │ └── 9.js └── Pratice ├── .gitignore ├── 1.js ├── 10.js ├── 11.js ├── 12.js ├── 13.js ├── 14.js ├── 15.js ├── 16.js ├── 17.js ├── 18.js ├── 19.js ├── 2.js ├── 20.js ├── 21.js ├── 22.js ├── 23.js ├── 24.js ├── 25.js ├── 26.js ├── 27.js ├── 28.js ├── 29.js ├── 3.js ├── 30.js ├── 31.js ├── 4.js ├── 5.js ├── 6.js ├── 7.js ├── 8.js ├── 9.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .vscode 3 | .cache 4 | .history 5 | -------------------------------------------------------------------------------- /2. Basic of JavaScript/10.js: -------------------------------------------------------------------------------- 1 | // Reassign the VAR in VARIABLE_NAME to a new value. ✅ 2 | var a = "apple"; 3 | a = "ball"; 4 | console.log(a); // ball // No Error ✅ 5 | 6 | var a = "apple"; 7 | var a = "applesss"; 8 | console.log(a); // applesss // No Error ✅ 9 | // ------------------------------------------------------------------ 10 | // Reassign the LET in VARIABLE_ 11 | let b = "boat"; 12 | b = "banana"; 13 | console.log(b); // banana // No Error ✅ 14 | 15 | // let b = "boat"; 16 | // let b = "banana"; 17 | // console.log(b); // banana // Error - Cannot redeclare b ❌ 18 | 19 | //------------------------------------------------------------------ 20 | 21 | // const c = 3.14; 22 | // c = 45; 23 | // console.log(c); // 3.14 // Error - Cannot re-assign c ❌ 24 | 25 | // const c = 3.1457; 26 | // const c = 65; 27 | // console.log(c); // 3.1457 // Error - Cannot re-assign c ❌ -------------------------------------------------------------------------------- /2. Basic of JavaScript/5.js: -------------------------------------------------------------------------------- 1 | // Difference Between var and let and const 2 | 3 | // VAR 4 | 5 | // Use of this keyword without varibale declaration is a default of var 6 | 7 | // var name = 'Jack'; 8 | // name = 'Jack' // But this is not a good practice and its deprecated 9 | 10 | // 1.Scoping 11 | // 2. Reassignment 12 | // 3. When we access a vaiable without declaring 13 | 14 | // 1. Scoping 15 | // What is the scope of a variable? 16 | // 17 | // There are three types of scope 18 | // 1. Block Scope 19 | // 2. Function Scope 20 | // 3. Global Scope 21 | 22 | // 1. Block Scope 23 | { 24 | var x = "Hello"; 25 | } 26 | 27 | // 2. Function Scope 28 | function scope() { 29 | var x = "Hello"; 30 | } 31 | 32 | // 3. Global Scope 33 | var global = "I am Global"; 34 | 35 | // VAR USES 36 | 37 | // 1. but if var is in a function scope ...then it can be only available in that function scope 38 | // 2. if var is in global scope ...then it can be available in all the function scope and global scope 39 | // 3. if var is in a function scope ..it will become function scope variable 40 | // 4. if var is in a function scope .. you can acces it inside that function scope at any block...inside that function scope // Having it Multiple blocks 41 | // 5. Var allows you to re declare the same variable 42 | 43 | // WHY NOT TO USE VAR 44 | // 1. It is not a good practice to use var 45 | // 2. IF You asssign a value to a variable using var, it will be accessible in the global scope 46 | // 3. If you assign var i = 1 and same let i = 1 and then it will be conflict with let 47 | // 4. If you use var in a loop, it will be accessible in the global scope 48 | // 5. Because the let i will be print everywhere out of the block as it is having the previous value of i from var 49 | // 6 So let will become a global varibale...if u have same varibale using let and var 50 | // 7. So Stop using var to declare variables 51 | -------------------------------------------------------------------------------- /2. Basic of JavaScript/7.js: -------------------------------------------------------------------------------- 1 | // Block Scope of VAR LET CONST 2 | // Inside Block Scope ✅ ✅ ✅ 3 | // Outside Block Scope ✅ ❌ ❌ 4 | 5 | // VAR 6 | 7 | // Block Scope of VAR 8 | // VAR Block Scope Works Both Inside and Outside Scope 9 | // Inside Block Scope ✅ 10 | { 11 | var x = 10; 12 | var y = 20; 13 | console.log(x, y ,"VAR INSIDE BLOCK SCOPE ✅" ); 14 | } 15 | // Output :- 10,20 16 | 17 | // Outside Block Scope ✅ 18 | { 19 | var x = 10; 20 | var y = 20; 21 | } 22 | console.log(x, y,"VAR OUTSIDE BLOCK SCOPE ✅"); 23 | // Output :- 10,20 24 | 25 | // ------------------------------------------------------------------------------------- 26 | 27 | 28 | // Block Scope of LET 29 | // Inside Block Scope ✅ 30 | { 31 | let a = 30; 32 | let b = 40; 33 | console.log(a, b,"LET INSIDE BLOCK SCOPE ✅"); 34 | } 35 | 36 | // Outside Block Scope ❌ 37 | 38 | // { 39 | // let a = 30; 40 | // let b = 40; 41 | // } 42 | // console.log(a, b,"LET OUTSIDE BLOCK SCOPE ❌"); 43 | 44 | // ------------------------------------------------------------------------------------- 45 | 46 | // Block Scope of CONST 47 | 48 | // Inside Block Scope ✅ 49 | { 50 | const c = 50; 51 | const d = 60; 52 | console.log(c, d,'CONST INSIDE BLOCK SCOPE ✅'); 53 | } 54 | 55 | // Outside Block Scope ❌ 56 | // { 57 | // const c = 50; 58 | // const d = 60; 59 | 60 | // } 61 | // console.log(c, d,"CONST OUTSIDE BLOCK SCOPE ❌"); 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /2. Basic of JavaScript/8.js: -------------------------------------------------------------------------------- 1 | // Function Scope VAR LET CONST 2 | // Inside Function Scope ✅ ✅ ✅ 3 | // Outside Function Scope ❌ ❌ ❌ 4 | 5 | 6 | // VAR 7 | // Inside Block Scope ✅ 8 | function home() { 9 | var x = 10; 10 | console.log(x, "VAR INSIDE BLOCK SCOPE ✅"); 11 | } 12 | home(); 13 | 14 | // Outside Block Scope ❌ 15 | 16 | // function home2() { 17 | // var x = 10; 18 | // } 19 | // home2(); 20 | // console.log(x,"VAR OUTSIDE BLOCK SCOPE ❌"); 21 | 22 | // -------------------------------------------------------------------------------- 23 | 24 | // LET 25 | // Inside Block Scope ✅ 26 | function car() { 27 | let x = 11; 28 | console.log(x, "LET INSIDE BLOCK SCOPE ✅"); 29 | } 30 | car(); 31 | 32 | // function car2() { 33 | // let x = 11; 34 | // } 35 | // car2(); 36 | // console.log(x, "LET OUTSIDE BLOCK SCOPE ❌"); 37 | 38 | // -------------------------------------------------------------------------------- 39 | 40 | function house() { 41 | const x = 12; 42 | console.log(x, "CONST INSIDE BLOCK SCOPE ✅"); 43 | } 44 | house(); 45 | 46 | // function house2() { 47 | // const x = 12; 48 | // } 49 | // house2(); 50 | // console.log(x, "CONST OUTSIDE BLOCK SCOPE ❌"); -------------------------------------------------------------------------------- /2. Basic of JavaScript/9.js: -------------------------------------------------------------------------------- 1 | // Global Scope VAR LET CONST 2 | // Inside of the global Scope ✅ ✅ ✅ 3 | // Outside of the global Scope ✅ ✅ ✅ 4 | 5 | // VAR 6 | 7 | var a = "apple"; 8 | 9 | function tree() { 10 | console.log(a, "----Global Scope Works Anywhere in the global Scope✅"); 11 | } 12 | tree(); 13 | 14 | // LET 15 | 16 | let b = "banana"; 17 | 18 | function tree2() { 19 | console.log(b, "----Global Scope Works Anywhere in the global Scope✅"); 20 | } 21 | tree2(); 22 | 23 | // CONST 24 | 25 | const c = "chocolate"; 26 | 27 | function tree3() { 28 | console.log(c, "----Global Scope Works Anywhere in the global Scope✅"); 29 | } 30 | tree3(); 31 | -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/10.js: -------------------------------------------------------------------------------- 1 | // Difference Between While and Do While Loop and For Loop 2 | // while loop is a entry controlled loop 3 | // do while loop is a exit controlled loop 4 | // for loop is a entry controlled loop 5 | // 6 | // while loop is a loop which is used to iterate the code multiple times 7 | // do while loop is a loop which is used to iterate the code multiple times 8 | // for loop is a loop which is used to iterate the code multiple times 9 | // 10 | // While loop is used when you know the number of iterations 11 | // Do while loop is used when you dont know the number of iterations 12 | // For loop is used when you know the number of iterations 13 | // 14 | // In While Loop Control will not enter into the loop if the condition is false 15 | // In Do While Loop Control will enter into the loop atleast once even if the condition is false 16 | // In For Loop Control will not enter into the loop if the condition is false 17 | // 18 | // -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/12.js: -------------------------------------------------------------------------------- 1 | // JavaScript Continue Statement 2 | // The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. 3 | // 4 | // Syntax 5 | // continue; 6 | // continue [label]; 7 | // Parameter Values 8 | // Parameter Description 9 | // label Optional. Specifies the label name to continue to. 10 | // Description 11 | // The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. 12 | // 13 | // When you use continue without a label, it terminates the current iteration of the innermost enclosing while, do-while, or for loop, and continues execution of the loop with the next iteration. 14 | // 15 | // When you use continue with a label, it terminates the current iteration of the specified labeled statement. 16 | // 17 | // Note: The continue statement has no effect if it is used outside a loop. 18 | // 19 | // Tip: Use the debugger statement to debug JavaScript code. 20 | // 21 | // Tip: Use the break statement to jump out of a loop. 22 | // 23 | // Tip: Use the return statement to stop the execution of a function. 24 | // 25 | // Tip: Use the throw statement to create a custom error. 26 | // 27 | // Example 28 | // The following example uses continue to skip the value of 3: 29 | // 30 | // Example 31 | // var text = ""; 32 | // var i; 33 | // for (i = 0; i < 10; i++) { 34 | // if (i === 3) { continue; } 35 | // text += "The number is " + i + "
"; 36 | // } 37 | // document.getElementById("demo").innerHTML = text; 38 | // The result of the example will be: 39 | // 40 | // The number is 0 41 | // The number is 1 42 | // The number is 2 43 | // The number is 4 44 | // The number is 5 45 | // The number is 6 46 | // The number is 7 47 | // The number is 8 48 | // The number is 9 49 | // The following example uses continue to skip the value of 3: 50 | // 51 | -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/2.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | JavaScript if...else statement 5 | An if statement can have an optional else clause. The syntax of the if...else statement is: 6 | 7 | if (condition) { 8 | // block of code if condition is true 9 | } else { 10 | // block of code if condition is false 11 | } 12 | The if..else statement evaluates the condition inside the parenthesis. 13 | 14 | If the condition is evaluated to true, 15 | 16 | the code inside the body of if is executed 17 | the code inside the body of else is skipped from execution 18 | 19 | If the condition is evaluated to false, 20 | 21 | the code inside the body of else is executed 22 | the code inside the body of if is skipped from execution 23 | Working of if-else statement in JavaScript 24 | Working of the if...else statement 25 | Example 2: if…else Statement 26 | // check if the number is positive or negative/zero 27 | 28 | const number = prompt("Enter a number: "); 29 | 30 | // check if number is greater than 0 31 | if (number > 0) { 32 | console.log("The number is positive"); 33 | } 34 | // if number is not greater than 0 35 | else { 36 | console.log("The number is either a negative number or 0"); 37 | } 38 | 39 | console.log("The if...else statement is easy"); 40 | Run Code 41 | Output 1 42 | 43 | Enter a number: 2 44 | The number is positive 45 | The if...else statement is easy 46 | Suppose the user entered 2. In this case, the condition number > 0 evaluates to true. Hence, the body of the if statement is executed and the body of the else statement is skipped. 47 | 48 | Output 2 49 | 50 | Enter a number: -1 51 | The number is either a negative number or 0 52 | The if...else statement is easy 53 | Suppose the user entered -1. In this case, the condition number > 0 evaluates to false. Hence, the body of the else statement is executed and the body of the if statement is skipped. 54 | */ 55 | -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/4.js: -------------------------------------------------------------------------------- 1 | // Simple if statment 2 | // let num = 10; 3 | // if (num > 10) { 4 | // console.log("Hello"); 5 | // } 6 | // console.log("no"); 7 | 8 | let zero = 0; 9 | let minus = -1; 10 | let plus = 1; 11 | 12 | if (zero) { 13 | console.log("This is zero"); // 0 is false condition it will not enter the if statment 14 | } 15 | 16 | if (minus) { 17 | console.log("This is -1"); // this is not zero so it will print and go to true if statement 18 | } 19 | 20 | if (plus) { 21 | console.log("This is 1"); // this is not zero so it will print and go to true if statement 22 | } 23 | 24 | // Except 0 all numbers are false 25 | -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/5.js: -------------------------------------------------------------------------------- 1 | // if-else 2 | 3 | let num = 10; 4 | 5 | if (num % 2 == 0) { 6 | console.log("even",num); // if true condition pass then it will go it true statment 7 | } else { 8 | console.log("odd",num); 9 | } 10 | 11 | let num2 = 11; 12 | 13 | if (num2 % 2 == 0) { 14 | console.log("even",num2); 15 | } else { 16 | console.log("odd",num2); // if true fails condition pass then it will go it else block 17 | } 18 | -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/6.js: -------------------------------------------------------------------------------- 1 | // if ...else if ......else 2 | 3 | let num = 2; 4 | 5 | if (num > 0) { 6 | console.log("Positive"); 7 | } else if (num < 0) { 8 | // else if uses for multiple conditions 9 | console.log("Negative"); 10 | } else { 11 | console.log("Zero"); 12 | } 13 | 14 | // you can have many else if blocks 15 | // but it should be end with else block 16 | -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/7.js: -------------------------------------------------------------------------------- 1 | // Nested if else 2 | // Nested if...else Statement 3 | // You can also use an if...else statement inside of an if...else statement. This is known as nested if...else statement. 4 | 5 | // Example 4: Nested if...else Statement 6 | // check if the number is positive, negative or zero 7 | const number = prompt("Enter a number: "); 8 | 9 | if (number >= 0) { 10 | if (number == 0) { 11 | console.log("You entered number 0"); 12 | } else { 13 | console.log("You entered a positive number"); 14 | } 15 | } else { 16 | console.log("You entered a negative number"); 17 | } 18 | 19 | // Enter a number: 5 20 | // You entered a positive number -------------------------------------------------------------------------------- /3. Control Flow of JavaScript/8.js: -------------------------------------------------------------------------------- 1 | // Body of if...else With Only One Statement 2 | // If the body of if...else has only one statement, we can omit { } in our programs. For example, you can replace 3 | 4 | const number1 = 2; 5 | if (number1 > 0) { 6 | console.log("The number is positive."); 7 | } else { 8 | console.log("The number is negative or zero."); 9 | } 10 | 11 | const number2 = 2; 12 | if (number2 > 0) console.log("The number is positive.");// if there is single statment we can use it without curly braces 13 | else console.log("The number is negative or zero."); 14 | 15 | 16 | // The number is positive. 17 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/1.js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Function 3 | A function is a block of code that performs a specific task. 4 | 5 | Suppose you need to create a program to create a circle and color it. You can create two functions to solve this problem: 6 | 7 | a function to draw the circle 8 | a function to color the circle 9 | Dividing a complex problem into smaller chunks makes your program easy to understand and reusable. 10 | 11 | JavaScript also has a huge number of inbuilt functions. For example, Math.sqrt() is a function to calculate the square root of a number. 12 | 13 | In this tutorial, you will learn about user-defined functions. 14 | 15 | Declaring a Function 16 | The syntax to declare a function is: 17 | 18 | function nameOfFunction () { 19 | // function body 20 | } 21 | A function is declared using the function keyword. 22 | The basic rules of naming a function are similar to naming a variable. It is better to write a descriptive name for your function. For example, if a function is used to add two numbers, you could name the function add or addNumbers. 23 | The body of function is written within {}. 24 | For example, 25 | 26 | // declaring a function named greet() 27 | function greet() { 28 | console.log("Hello there"); 29 | } 30 | Calling a Function 31 | In the above program, we have declared a function named greet(). To use that function, we need to call it. 32 | 33 | Here's how you can call the above greet() function. 34 | 35 | // function call 36 | greet(); 37 | Working of JavaScript function 38 | Working of a Function in JavaScript 39 | 40 | 41 | Example 1: Display a Text 42 | // program to print a text 43 | // declaring a function 44 | function greet() { 45 | console.log("Hello there!"); 46 | } 47 | 48 | // calling the function 49 | greet(); 50 | Run Code 51 | Output 52 | 53 | Hello there! 54 | 55 | 56 | */ -------------------------------------------------------------------------------- /4. Functions in JavaScript/10.js: -------------------------------------------------------------------------------- 1 | // Factorial 2 | 3 | // 5! = 5x(5-1)x(5-2)x(5-3)x(5-4)x(5-5) 4 | // 5! = 5x4x3x2x1 5 | let ans = 1; 6 | function fac(num) { 7 | for (let i = num; i >= 1; i--) { 8 | ans *= i; 9 | } 10 | console.log(ans); 11 | } 12 | fac(4); 13 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/11.js: -------------------------------------------------------------------------------- 1 | let ans = 1; 2 | function fac(num) { 3 | for (let i = 1; i <= num; i++) { 4 | ans *= i; 5 | } 6 | console.log(ans); 7 | } 8 | fac(0); 9 | 10 | function fac1(num) { 11 | return num == 1 || num == 0 ? 1 : num * fac1(num - 1); 12 | } 13 | let n = 5; 14 | console.log(fac1(n)); 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/12.js: -------------------------------------------------------------------------------- 1 | function fac(num) { 2 | return num == 1 || num == 0 ? 1 : num * fac(num - 1); 3 | } 4 | 5 | let n = 10; 6 | console.log(fac(n)); 7 | 8 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/13.js: -------------------------------------------------------------------------------- 1 | // using the + operator 2 | const message1 = 3 | "This is a long message " + 4 | "that spans across multiple lines" + 5 | "in the code."; 6 | 7 | // using the \ operator 8 | const message2 = 9 | "This is a long message \ 10 | that spans across multiple lines \ 11 | in the code."; 12 | 13 | // using the template literal 14 | const message3 = `This is a long message 15 | that spans across multiple lines 16 | in the code.`; 17 | 18 | 19 | console.log(message1); 20 | console.log(message2); 21 | console.log(message3); 22 | 23 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/14.js: -------------------------------------------------------------------------------- 1 | // Anonymous Function 2 | 3 | setTimeout(function () { 4 | console.log("Hello"); 5 | }, 3000); 6 | 7 | // This is an Anonymous Function 8 | // A Function without Name is called Anonymous Function 9 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/15.js: -------------------------------------------------------------------------------- 1 | // IIFE 2 | // Immediately Invoked Function Expression 3 | // 4 | (function () { 5 | console.log("Hello"); 6 | })(); 7 | // 8 | // // This is an IIFE 9 | // // A Function without Name is called Anonymous Function 10 | // // IIFE is an Anonymous Function 11 | // // IIFE is an Anonymous Function that is immediately invoked 12 | // When do we use IIFE? 13 | // When we want to execute a function immediately 14 | // When we want to create a private scope 15 | // When we want to create a private variable 16 | // When we want to create a private function 17 | // When we want to create a private method 18 | // When we want to create a private property 19 | // When we want to create a private class 20 | // When we want to create a private object 21 | // When we want to create a private instance 22 | // When we want to create a private constructor 23 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/16.js: -------------------------------------------------------------------------------- 1 | function ShowMessage(firstName, lastName) { 2 | console.log("Hello " + arguments[0] + " " + arguments[1]); 3 | } 4 | 5 | ShowMessage("Steve", "Jobs"); 6 | 7 | ShowMessage("Bill", "Gates"); 8 | 9 | ShowMessage(100, 200); 10 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/17.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/4. Functions in JavaScript/17.js -------------------------------------------------------------------------------- /4. Functions in JavaScript/18.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/4. Functions in JavaScript/18.js -------------------------------------------------------------------------------- /4. Functions in JavaScript/19.js: -------------------------------------------------------------------------------- 1 | function fac(num){ 2 | return num = 1 || num == 0 ? 1 : num * fac(num-1) 3 | } 4 | let n = 10; 5 | console.log(fac(n)) -------------------------------------------------------------------------------- /4. Functions in JavaScript/2.js: -------------------------------------------------------------------------------- 1 | /* 2 | A Simple JavaScript Function 3 | */ 4 | 5 | function greet() { 6 | // Declaring a Function 7 | console.log("Hello there"); 8 | } 9 | greet(); // Calling a Function 10 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/20.js: -------------------------------------------------------------------------------- 1 | // Fac 2 | 3 | function fac(num) { 4 | let sum = 1; 5 | for (let i = num; i >= 1; i--) { 6 | sum *= i; 7 | } 8 | console.log(sum); 9 | } 10 | fac(10); 11 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/3.js: -------------------------------------------------------------------------------- 1 | var square = function (x) { 2 | var result = x * x; 3 | console.log(result); 4 | }; 5 | square(3); 6 | 7 | /* 8 | 9 | 10 | There is actually a very clear distinction between these calls. 11 | console.log() is a function used to print information to the console. 12 | return on the other hand is a call to pass some value back up to where the call was made. 13 | For instance, let’s say you create a function called square() that takes in a single numeric parameter and returns that value squared. 14 | Your function would look like the following: 15 | 16 | var square = function square(x) { 17 | return x*x; 18 | }; 19 | You would call your function like this: var ans = square(5); 20 | 21 | This would set ans equal to the value returned by square() but will not print it to the console. 22 | Hence, the return statement is passing the value x*x back to the call of square. 23 | If you want to view the value in ans, you would write: console.log(ans); or to simplify the problem as a whole you could write: console.log(square(5)); and my guess is you have this last line in your code which is why you are confused. 24 | 25 | Hope this helps, Dustin =D 26 | 27 | 28 | 29 | 30 | */ 31 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/4.js: -------------------------------------------------------------------------------- 1 | /* 2 | A JavaScript Function with a Return Statement 3 | 1. A function can also return a value. 4 | 2. The return statement is used to return a value from a function. 5 | 3. The return statement ends function execution and specifies a value to be returned to the function caller. 6 | 4. The return statement can be used to return a value from a function. 7 | 5. After the return statement (if any), the function will stop executing. 8 | 6. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. 9 | 7. Functions often compute a return value. The return value is "returned" back to the "caller": 10 | 8. The code that called the function can "receive" the return value in a variable. 11 | 9. The return statement can be used without a value. In this case, the function will return undefined. 12 | 10.Return values are often used in expressions.The return value of a function can be used as a value for a variable: 13 | */ 14 | 15 | function sum(a, b) { 16 | console.log(a + b); 17 | } 18 | let total = sum(3, 3); 19 | console.log(total); 20 | // 21 | // Output: 22 | // 6 23 | // undefined 24 | 25 | 26 | // To remove that undefined we use the return statement 27 | // If we dont use the return statement then the function will return undefined 28 | // If we use the return statement then the function will return the value 29 | // If we dont use the return it will not allow the function to store the variable in next variable 30 | // If we use the return it will allow the function to store the variable in next variable..without getting undefined 31 | 32 | function sum2(a, b) { 33 | return a + b; 34 | } 35 | let total1 = sum2(3, 3); 36 | console.log(total1); 37 | // 38 | // Output: 39 | // 6 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/5.js: -------------------------------------------------------------------------------- 1 | // Normal Console.log 2 | 3 | function test1() { 4 | console.log("Hello World"); 5 | } 6 | test1(); 7 | 8 | // Return Console.log 9 | // Function Return 10 | // The return statement can be used to return the value to a function call. 11 | 12 | // The return statement denotes that the function has ended. Any code after return is not executed. 13 | 14 | // If nothing is returned, the function returns an undefined value. 15 | function test2(a, b) { 16 | return a + b; 17 | 18 | } 19 | var sum = test2(2, 3); 20 | console.log(sum); 21 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/9.js: -------------------------------------------------------------------------------- 1 | let print = function (number) { 2 | let n1 = number - 1; 3 | console.log(n1); 4 | if (n1 > 0) { 5 | print(n1); 6 | } 7 | }; 8 | print(5); 9 | -------------------------------------------------------------------------------- /4. Functions in JavaScript/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/1.js: -------------------------------------------------------------------------------- 1 | // Objects in JavaScript 2 | // 1. Objects in JavaScript 3 | // 2. Object Literals 4 | // 3. Object Properties 5 | // 4. Object Methods 6 | // 5. The "this" Keyword 7 | // 6. Object Constructors 8 | // 7. Object Prototypes 9 | // 8. Object Inheritance 10 | // 9. Object Iteration 11 | // 10. Object Equality 12 | // 11. Object Cloning 13 | // 12. Object References 14 | 15 | 16 | // What is an object? 17 | // An object is a collection of properties 18 | // An object is a collection of key-value pairs 19 | // An object is a collection of name-value pairs 20 | // An object is a Non-Primitive Data Type 21 | // An object is a Complex Data Type 22 | // An object is a Reference Data Type 23 | // An object is usually declared using curly braces {} -------------------------------------------------------------------------------- /5. Objects in JavaScript/10.js: -------------------------------------------------------------------------------- 1 | let x = new Date() 2 | console.log(x) 3 | // console.log(x.getFullYear()); 4 | // console.log(x.getMonth()); 5 | // console.log(x.getDate()); 6 | // console.log(x.getDay()); 7 | // console.log(x.getTime()); 8 | // console.log(x.getHours()); 9 | // console.log(x.getMinutes()); 10 | // console.log(x.getSeconds()); 11 | // console.log(x.getMilliseconds()); 12 | // console.log(x.getTimezoneOffset()); 13 | // console.log(x.getUTCDate()); 14 | // console.log(x.getUTCDay()); 15 | // console.log(x.getUTCFullYear()); 16 | // console.log(x.getUTCHours()); 17 | // console.log(x.getUTCMilliseconds()); 18 | // console.log(x.getUTCMinutes()); 19 | // console.log(x.getUTCMonth()); 20 | // console.log(x.getUTCSeconds()); 21 | 22 | x.setDate(12); 23 | console.log(x) 24 | 25 | console.log(x.getDate()); 26 | x.setFullYear(2003); 27 | console.log(x); 28 | x.setHours(12); 29 | console.log(x); 30 | x.setMilliseconds(12); 31 | console.log(x); 32 | x.setMinutes(12); 33 | console.log(x); 34 | x.setMonth(12); 35 | console.log(x); 36 | x.setSeconds(12); 37 | console.log(x); 38 | x.setTime(12); 39 | console.log(x); 40 | x.setUTCDate(12); 41 | console.log(x); 42 | x.setUTCFullYear(2003); 43 | console.log(x); 44 | x.setUTCHours(12); 45 | console.log(x); 46 | x.setUTCMilliseconds(12); 47 | console.log(x); 48 | x.setUTCMinutes(12); 49 | console.log(x); 50 | x.setUTCMonth(12); 51 | console.log(x); 52 | x.setUTCSeconds(12); 53 | console.log(x); 54 | 55 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/11.js: -------------------------------------------------------------------------------- 1 | let date = new Date().toLocaleString( 2 | "en-us", 3 | { weekday: "long" }, 4 | { timeZone: "Asia/Kolkata" } 5 | ); 6 | console.log(date); 7 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/12.js: -------------------------------------------------------------------------------- 1 | let now = new Date().toLocaleString("en-us", { 2 | month: "long", 3 | // day: "numeric", 4 | // year: "numeric", 5 | timeZone: "Asia/Kolkata", 6 | weekday: "long", 7 | hour: "numeric", 8 | minute: "numeric", 9 | second: "numeric", 10 | hour12: true, 11 | dayPeriod: "short", 12 | day:"2-digit", 13 | year:"2-digit" 14 | 15 | 16 | 17 | }); 18 | console.log(now); 19 | 20 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/15.js: -------------------------------------------------------------------------------- 1 | // Methods in JavaScript and This Keyword 2 | // Function in Object are Known as Methods 3 | //Hence basically, the JavaScript method is an object property that has a function value. 4 | 5 | // const obj = { 6 | // name: "Suhail", 7 | // age: 23, 8 | // isAlive: true, 9 | // call: function () { 10 | // return "Hello I am a Method" 11 | // } 12 | // } 13 | 14 | // console.log(obj); 15 | // console.log(obj.call()); 16 | 17 | 18 | // const obj = { 19 | // name: "Suhail", 20 | // age: 23, 21 | // isAlive: true, 22 | // fun() { 23 | // return "Hello I am a Method" 24 | // } 25 | // } 26 | 27 | // console.log(obj); 28 | // console.log(obj.fun()); 29 | 30 | 31 | const obj = { 32 | name: "Suhail", 33 | age: 23, 34 | isAlive: true, 35 | fun() { 36 | return `I am ${this.name} and my age is ${this.age} and i am alive is ${this.isAlive} ` 37 | } 38 | } 39 | 40 | console.log(obj); 41 | console.log(obj.fun()); 42 | // Output 43 | // { name: 'Suhail', age: 23, isAlive: true, fun: [Function: fun] } 44 | // I am Suhail and my age is 23 and i am alive is true -------------------------------------------------------------------------------- /5. Objects in JavaScript/16.js: -------------------------------------------------------------------------------- 1 | // JavaScript Constructor Function 2 | 3 | // function Name() { 4 | // this.name = "Suhail", 5 | // this.age = 20 6 | // } 7 | 8 | 9 | // const name = new Name(); 10 | 11 | // console.log(name); 12 | 13 | // const name2 = { 14 | // name: "sUHAIL", 15 | // age: 20 16 | // } 17 | 18 | // console.log(name2); 19 | 20 | 21 | function Tariq(date, month, year) { 22 | this.date = new Date().getDate() 23 | this.month = new Date().getMonth() + 1 24 | this.year = new Date().getFullYear() 25 | } 26 | 27 | const tariq = new Tariq() 28 | console.log(tariq); 29 | 30 | console.log(tariq.date); 31 | console.log(tariq.month); -------------------------------------------------------------------------------- /5. Objects in JavaScript/17.js: -------------------------------------------------------------------------------- 1 | function Tariq(date, month, year) { 2 | this.date = new Date().getDate() 3 | this.month = new Date().getMonth() + 1 4 | this.year = new Date().getFullYear() 5 | } 6 | 7 | const tariq = new Tariq() 8 | const date2 = new Tariq() 9 | console.log(tariq); 10 | console.log(date2); 11 | 12 | 13 | console.log(tariq.date); 14 | console.log(tariq.month); 15 | 16 | console.log(date2.date); 17 | console.log(date2.month); 18 | 19 | 20 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/18.js: -------------------------------------------------------------------------------- 1 | function Tariq(date, month, year) { 2 | this.date = date 3 | this.month = month 4 | this.year = year 5 | } 6 | 7 | const tariq = new Tariq(11, 02, 2002) 8 | const date2 = new Tariq(01, 10, 2022) 9 | console.log(tariq); 10 | console.log(date2); 11 | 12 | 13 | console.log(tariq.date); 14 | console.log(tariq.month); 15 | 16 | console.log(date2.date); 17 | console.log(date2.month); -------------------------------------------------------------------------------- /5. Objects in JavaScript/19.js: -------------------------------------------------------------------------------- 1 | // using object literal 2 | let person = { 3 | name: 'Sam' 4 | } 5 | // using constructor function 6 | // function Person() { 7 | // this.name = 'Sam' 8 | // } 9 | 10 | // let person1 = new Person(); 11 | // let person2 = new Person(); 12 | 13 | 14 | // using constructor function 15 | // function Person() { 16 | // this.name = 'Sam' 17 | // } 18 | 19 | // let person1 = new Person(); 20 | // let person2 = new Person(); 21 | 22 | // // adding new property to person1 23 | // person1.age = 20; 24 | 25 | 26 | // using object lateral 27 | // let person = { 28 | // name: 'Sam' 29 | // } 30 | 31 | // console.log(person.name); // Sam 32 | 33 | // let student = person; 34 | 35 | // // changes the property of an object 36 | // student.name = 'John'; 37 | 38 | // // changes the origins object property 39 | // console.log(person.name); // John -------------------------------------------------------------------------------- /5. Objects in JavaScript/2.js: -------------------------------------------------------------------------------- 1 | const obj = {}; 2 | console.log(obj); 3 | // Output: 4 | // {} Empty Object 5 | 6 | const person = { 7 | firstName: "Steve", 8 | lastName: "Jobs", 9 | age: 56, 10 | isMarried: true, 11 | address: { 12 | city: "San Francisco", 13 | state: "California", 14 | country: "USA", 15 | }, 16 | skills: ["HTML", "CSS", "JavaScript", "React", "Node", "MongoDB"], 17 | getFullName: function () { 18 | return this.firstName + " " + this.lastName; 19 | }, 20 | }; 21 | console.log(person,person.getFullName()); 22 | console.log(typeof person); // object 23 | 24 | 25 | // Accessing Object Properties 26 | // 1. Dot Notation 27 | // 2. Bracket Notation 28 | 29 | // Dot Notation 30 | console.log(person.firstName); 31 | console.log(person.lastName); 32 | 33 | // Bracket Notation 34 | console.log(person["firstName"]); 35 | console.log(person["lastName"]); 36 | 37 | 38 | // Modify Object Properties 39 | // 1. Dot Notation 40 | // 2. Bracket Notation 41 | 42 | // Dot Notation 43 | person.firstName = "Bill"; 44 | person.lastName = "Gates"; 45 | 46 | // Bracket Notation 47 | person["firstName"] = "Bill"; 48 | person["lastName"] = "Gates"; 49 | 50 | console.log(person); 51 | 52 | 53 | // Add New Properties 54 | // 1. Dot Notation 55 | // 2. Bracket Notation 56 | 57 | // Dot Notation 58 | person.firstName = "Bill"; 59 | person.lastName = "Gates"; 60 | person.age = 65; 61 | 62 | // Bracket Notation 63 | person["firstName"] = "Bill"; 64 | person["lastName"] = "Gates"; 65 | person["age"] = 65; 66 | 67 | console.log(person); 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | // person.life = true 83 | // console.log(person.life); 84 | // delete person.life 85 | // console.log(person.life); 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/20.js: -------------------------------------------------------------------------------- 1 | // // constructor function 2 | // function Person() { 3 | // this.name = 'John', 4 | // this.age = 23 5 | // } 6 | 7 | // // creating objects 8 | // let person1 = new Person(); 9 | // let person2 = new Person(); 10 | 11 | // // adding property to person1 object 12 | // person1.gender = 'male'; 13 | 14 | // // adding method to person1 object 15 | // person1.greet = function () { 16 | // console.log('hello'); 17 | // } 18 | 19 | // person1.greet(); // hello 20 | 21 | // // Error code 22 | // // person2 doesn't have greet() method 23 | // person2.greet(); -------------------------------------------------------------------------------- /5. Objects in JavaScript/21.js: -------------------------------------------------------------------------------- 1 | // JavaScript Object Prototype 2 | // You can also add properties and methods to a constructor function using a prototype.For example, 3 | 4 | // // constructor function 5 | // function Person() { 6 | // this.name = 'John', 7 | // this.age = 23 8 | // } 9 | 10 | // // creating objects 11 | // let person1 = new Person(); 12 | // let person2 = new Person(); 13 | 14 | // // adding new property to constructor function 15 | // Person.prototype.gender = 'Male'; 16 | 17 | // console.log(person1.gender); // Male 18 | // console.log(person2.gender); // Male -------------------------------------------------------------------------------- /5. Objects in JavaScript/22.js: -------------------------------------------------------------------------------- 1 | // JavaScript Built -in Constructors 2 | // JavaScript also has built -in constructors.Some of them are: 3 | 4 | // let a = new Object(); // A new Object object 5 | // let b = new String(); // A new String object 6 | // let c = new Number(); // A new Number object 7 | // let d = new Boolean(); // A new Boolean object 8 | // In JavaScript, strings can be created as objects by: 9 | 10 | // const name = new String('John'); 11 | // console.log(name); // "John" 12 | // Run Code 13 | // In JavaScript, numbers can be created as objects by: 14 | 15 | // const number = new Number(57); 16 | // console.log(number); // 57 17 | // Run Code 18 | // In JavaScript, booleans can be created as objects by: 19 | 20 | // const count = new Boolean(true); 21 | // console.log(count); // true 22 | // Run Code 23 | // Note: It is recommended to use primitive data types and create them in a normal way, such as const name = 'John';, const number = 57; and const count = true; 24 | 25 | // You should not declare strings, numbers, and boolean values as objects because they slow down the program. 26 | 27 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/23.js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Getter and Setter 3 | In this tutorial, you will learn about JavaScript getter and setter methods with the help of examples. 4 | 5 | In JavaScript, there are two kinds of object properties: 6 | 7 | Data properties 8 | Accessor properties 9 | Data Property 10 | Here's an example of data property that we have been using in the previous tutorials. 11 | 12 | const student = { 13 | 14 | // data property 15 | firstName: 'Monica'; 16 | }; 17 | Accessor Property 18 | In JavaScript, accessor properties are methods that get or set the value of an object. For that, we use these two keywords: 19 | 20 | get - to define a getter method to get the property value 21 | set - to define a setter method to set the property value 22 | 23 | 24 | */ 25 | 26 | const person = { 27 | name: "Suhail", 28 | age: 20, 29 | fac() { 30 | return `I am ${this.name} age of ${this.age}` 31 | } 32 | } 33 | 34 | 35 | console.log(person); 36 | console.log(person.fac()); // If you dont want to add () after fac then add get in function 37 | 38 | // Example 39 | 40 | const person1 = { 41 | name: "Suhail", 42 | age: 20, 43 | get fac() { 44 | return `I am ${this.name} age of ${this.age}` 45 | } 46 | } 47 | 48 | 49 | console.log(person1); 50 | console.log(person1.fac); // If you add fac() then u will get error 51 | 52 | // Using get because you can use like a function other key value pairs like person1.name and person1.fac 53 | // where you dont need to call a function like person1.fac() 54 | 55 | 56 | 57 | 58 | 59 | // get getName() { 60 | // return this.firstName; 61 | // } 62 | // // Note: To create a getter method, the get keyword is used. 63 | 64 | // // And also when accessing the value, we access the value as a property. 65 | 66 | // student.getName; 67 | 68 | // console.log(student.getName()); // error -------------------------------------------------------------------------------- /5. Objects in JavaScript/24.js: -------------------------------------------------------------------------------- 1 | // JavaScript Setter 2 | // In JavaScript, setter methods are used to change the values of an object.For example, 3 | /* 4 | 5 | 6 | const student = { 7 | firstName: 'Monica', 8 | 9 | //accessor property(setter) 10 | set changeName(newName) { 11 | this.firstName = newName; 12 | } 13 | }; 14 | 15 | console.log(student.firstName); // Monica 16 | 17 | // change(set) object property using a setter 18 | student.changeName = 'Sarah'; 19 | 20 | console.log(student.firstName); // Sarah 21 | 22 | 23 | */ 24 | 25 | const student = { 26 | firstname: "Suhail" 27 | } 28 | 29 | Object.defineProperty(student, "getName", { 30 | get: function () { 31 | return this.firstname 32 | } 33 | }) 34 | 35 | Object.defineProperty(student, "setName", { 36 | set: function (value) { 37 | return this.firstname = value; 38 | } 39 | 40 | }) 41 | 42 | student.setName = "Hello" 43 | // console.log(s); 44 | console.log(student.getName); 45 | 46 | 47 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/25.js: -------------------------------------------------------------------------------- 1 | // constructor function 2 | function Person() { 3 | this.name = 'John', 4 | this.age = 23 5 | } 6 | 7 | // creating objects 8 | const person1 = new Person(); 9 | const person2 = new Person(); 10 | 11 | // adding property to constructor function 12 | Person.prototype.gender = 'male'; 13 | 14 | // prototype value of Person 15 | console.log(Person.prototype); 16 | 17 | // inheriting the property from prototype 18 | console.log(person1.gender); 19 | console.log(person2.gender); -------------------------------------------------------------------------------- /5. Objects in JavaScript/26.js: -------------------------------------------------------------------------------- 1 | function Person() { 2 | } 3 | 4 | // adding property 5 | Person.prototype.age = 23 6 | 7 | const person1 = new Person(); 8 | Person.prototype.name = 'Peter'; 9 | 10 | console.log(person1.name); // John 11 | console.log(person1.age); // 23 -------------------------------------------------------------------------------- /5. Objects in JavaScript/27.js: -------------------------------------------------------------------------------- 1 | function Person() { 2 | this.name = 'John' 3 | } 4 | 5 | // adding a prototype 6 | Person.prototype.num = 23; 7 | 8 | // creating object 9 | const person = new Person(); 10 | 11 | // accessing prototype property 12 | console.log(person); 13 | 14 | console.log(person.__proto__); // { age: 24 } -------------------------------------------------------------------------------- /5. Objects in JavaScript/28.js: -------------------------------------------------------------------------------- 1 | const obj = { 2 | name: "Suhail", 3 | age: 20, 4 | isAlive : true 5 | } 6 | console.log(obj); 7 | 8 | console.log(Object.keys(obj)); 9 | console.log(Object.values(obj)); 10 | console.log(Object.entries(obj)); 11 | console.log(Object.getOwnPropertyNames(obj)); 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/3.js: -------------------------------------------------------------------------------- 1 | const empty = {}; 2 | console.log(empty); 3 | 4 | empty.firstName = "Steve"; 5 | empty.lastName = "Jobs"; 6 | // 7 | // now we have a new property called firstName 8 | // now we have a new property called lastName 9 | console.log(empty); 10 | 11 | // Update 12 | empty.firstName = "Bill"; 13 | empty.lastName = "Gates"; 14 | console.log(empty); 15 | // now we have a updated property called firstName 16 | // now we have a updated property called lastName 17 | 18 | // Delete 19 | delete empty.firstName; 20 | delete empty.lastName; 21 | 22 | console.log(empty); 23 | // now we have a deleted property called firstName 24 | // now we have a deleted property called lastName 25 | 26 | // To check if a property exists in an object 27 | 28 | empty.firstName = "Bill"; 29 | empty.lastName = "Gates"; 30 | // in operator 31 | // in operator returns true if the property exists in the object 32 | // in operator returns false if the property does not exist in the object 33 | 34 | console.log("firstName" in empty); 35 | console.log("lastName" in empty); 36 | // age does not exist in the empty object 37 | // so it returns false 38 | console.log("age" in empty); 39 | 40 | // Iterating over an object 41 | // for in loop 42 | // for in loop iterates over the properties of an object 43 | // for in loop iterates over the keys of an object 44 | 45 | for (let key in empty) { 46 | // where key is the name of the property 47 | // where empty[key] is the value of the property 48 | 49 | console.log(key, empty[key]); 50 | } 51 | 52 | 53 | // In Loops of Objects use Arrow Notation 54 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/4.js: -------------------------------------------------------------------------------- 1 | // Methods in Objects 2 | 3 | // If a Function is a Property of an Object, it is called a Method 4 | 5 | // 4 Ways to Create Methods in Objects 6 | 7 | const person = { 8 | firstName: "Steve", 9 | lastName: "Jobs", 10 | }; 11 | 12 | // 1. While Creating an Object 13 | 14 | const person1 = { 15 | firstName: "Steve", 16 | lastName: "Jobs", 17 | fun: function () { 18 | return "Hello"; 19 | }, 20 | }; 21 | console.log(person, person1.fun()); 22 | 23 | // Output 24 | // { firstName: 'Steve', lastName: 'Jobs' } Hello 25 | 26 | // 2. After Creating an Object 27 | 28 | const person2 = { 29 | firstName: "Steve", 30 | lastName: "Jobs", 31 | }; 32 | 33 | person2.fun = function () { 34 | return "Hello"; 35 | }; 36 | 37 | console.log(person2, person2.fun()); 38 | 39 | // Output 40 | // { firstName: 'Steve', lastName: 'Jobs', fun: [Function: fun] } Hello 41 | 42 | // 3. Create a Function and then Add it to an Object 43 | 44 | const person3 = { 45 | firstName: "Steve", 46 | lastName: "Jobs", 47 | }; 48 | 49 | function fun() { 50 | return "Hello"; 51 | } 52 | 53 | person3.fun = fun; 54 | 55 | console.log(person3, person3.fun()); 56 | 57 | // Output 58 | // { firstName: 'Steve', lastName: 'Jobs', fun: [Function: fun] } Hello 59 | 60 | // 4. Add a Function to an Object 61 | 62 | const person4 = { 63 | firstName: "Steve", 64 | lastName: "Jobs", 65 | fun() { 66 | return "Hello"; 67 | }, 68 | }; 69 | 70 | console.log(person4, person4.fun()); 71 | 72 | // Output 73 | // { firstName: 'Steve', lastName: 'Jobs', fun: [Function: fun] } Hello 74 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/5.js: -------------------------------------------------------------------------------- 1 | const person = { 2 | name: "Steve", 3 | age: 56, 4 | isMarried: true, 5 | address: { 6 | city: "San Francisco", 7 | state: "California", 8 | country: "USA \n", 9 | }, 10 | fun: function () { 11 | return "\n Hello \n"; 12 | }, 13 | phone: 1234567890, 14 | }; 15 | 16 | 17 | person.say = function () { 18 | return "Hello Say"; 19 | } 20 | console.log(person,person.fun(),person.say()); 21 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/6.js: -------------------------------------------------------------------------------- 1 | // Using this keyword in JavaScript 2 | // 3 | // What is this keyword? 4 | // this keyword refers to the object it belongs to 5 | // this keyword refers to the owner of the function 6 | // this keyword refers to the current object 7 | // this keyword can have different values depending on where it is used 8 | // this keyword is not static 9 | 10 | // In a method, this refers to the owner object 11 | // In a function, this refers to the global object 12 | // In a function, in strict mode, this is undefined 13 | 14 | const person = { 15 | firstName: "Steve", 16 | lastName: "Jobs", 17 | age: 56, 18 | fun() { 19 | return ( 20 | "Hello " + 21 | this.firstName + 22 | this.lastName + 23 | " and i have car of " + 24 | car.name 25 | ); 26 | // `Hello ${person.firstName} ${person.lastName} and i have car of ${car.name} 27 | // Instaed of using person we can use this keyword 28 | // for other objects you have use there key with value 29 | // You can use this keyword inside its own object 30 | }, 31 | }; 32 | 33 | const car = { 34 | name: "BMW", 35 | model: "X5", 36 | year: 2020, 37 | }; 38 | console.log(person,person.fun()); 39 | 40 | // In a above Object of person we are using person every where in the function 41 | // so we can use this keyword instead of person 42 | // Two Objets casn share the key value pair to each other 43 | // this keyword is used to refer to the object it belongs to it 44 | -------------------------------------------------------------------------------- /5. Objects in JavaScript/8.js: -------------------------------------------------------------------------------- 1 | // Math Random 2 | 3 | // Math.random() 4 | // The Math.random() function returns a random number between 0 (inclusive), and 1 (exclusive): 5 | // 6 | // console.log(Math.random()); // 0.1234567894322 7 | // console.log(Math.random()); // 0.987654321 8 | 9 | // for (let i = 0; i < 100; i++) { 10 | // let random = Math.random(); 11 | // console.log(Math.floor(random * (10 - 9)) + 9); 12 | // } 13 | 14 | // (max - min )+ min 15 | 16 | // Function to generate random number 17 | // function getRandomNumber(min, max) { 18 | // return Math.floor(Math.random() * (max - min + 1) + min); 19 | // } 20 | 21 | // console.log(getRandomNumber(10, 11)); 22 | 23 | 24 | 25 | function random(min,max){ 26 | return Math.floor(Math.random()*(max-min + 1)+ min); 27 | } 28 | console.log(random(1,4)) 29 | // random(1,4) -------------------------------------------------------------------------------- /5. Objects in JavaScript/9.js: -------------------------------------------------------------------------------- 1 | // Date in JavaScript 2 | 3 | let x = new Date(); 4 | console.log(x); 5 | // Output 6 | // 2021-07-22T06:56:00.000Z in Node JS its in utc iso string 7 | 8 | // Output 9 | // Mon Sep 12 2022 04:21:33 GMT+0530 (India Standard Time) in Browser 10 | 11 | // Parameters in Date 12 | // Maximum 7 Parameters 13 | // new Date(year, month, day, hours, minutes, seconds, milliseconds) 14 | let y = new Date(2002, 01, 01, 01, 01, 01, 01); 15 | console.log(y); 16 | // Output 17 | 18 | // Minimum 2 Parameter which is year 19 | // new Date(year) 20 | let z = new Date(2002, 01); 21 | console.log(z); 22 | 23 | let a = new Date("October 13, 2014 11:13:00"); 24 | let b = new Date("October 13, 2014"); 25 | let c = new Date("2014-11-13"); 26 | let d = new Date(1410692980000); 27 | console.log(a); 28 | 29 | // Start Date of the Date 30 | let e = new Date(0); 31 | console.log(e); 32 | // Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time) 33 | 34 | -------------------------------------------------------------------------------- /6. Types in JavaScript/10.js: -------------------------------------------------------------------------------- 1 | const arr = ["zero", "one", "two"] 2 | 3 | console.log(Object.assign({}, arr)); 4 | 5 | const arr2 = [["name", "Suhail"], ['age', 20], ["isAlive", true]] 6 | console.log(Object.fromEntries(arr2)); 7 | 8 | -------------------------------------------------------------------------------- /6. Types in JavaScript/11.js: -------------------------------------------------------------------------------- 1 | // Multidimensional Arrays 2 | 3 | let arr = [["Jack", 24], ["Sarah", 23]] 4 | console.log(arr); 5 | 6 | let student1 = ["student1", 20] 7 | let student2 = ["student2", 23] 8 | let student3 = ["student3", 34] 9 | 10 | let studentData = [student1, student2, student3]; 11 | console.log(studentData); 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /6. Types in JavaScript/12.js: -------------------------------------------------------------------------------- 1 | // Adding elements in 2d Array == Multidimensional Array 2 | 3 | let arr = [["Jack", 23], ["Sarah", 24]] 4 | arr.push(["Suhail", 21]) 5 | console.log(arr); // [ [ 'Jack', 23 ], [ 'Sarah', 24 ], [ 'Suhail', 21 ] ] 6 | 7 | 8 | let arr2 = [["Jack", 23], ["Sarah", 24]] 9 | arr2[1][2] = "Suhail"; 10 | console.log(arr2); // [ [ 'Jack', 23 ], [ 'Sarah', 24, 'Suhail' ] ] 11 | 12 | 13 | let arr3 = [['Jack', 23], ['Sarah', 24, 'Suhail']] 14 | arr3.pop() 15 | console.log(arr3); // [ [ 'Jack', 23 ] ] 16 | 17 | 18 | let arr4 = [['Jack', 23], ['Sarah', 24, 'Suhail']] 19 | arr4[1].pop() 20 | console.log(arr4); 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /6. Types in JavaScript/13.js: -------------------------------------------------------------------------------- 1 | // Array Methods 2 | /* 3 | JavaScript Array Methods 4 | 5 | 6 | 1.JavaScript concat() 7 | 2.JavaScript constructor() 8 | 3.JavaScript copyWithin() 9 | 4.JavaScript entries() 10 | 5.JavaScript every() 11 | 6.JavaScript fill() 12 | 7.JavaScript filter() 13 | 8.JavaScript find() 14 | 9.JavaScript findIndex() 15 | 10.JvaScript flat() 16 | 17 | 11.JvaScript flatMap() 18 | 12.J.vaScript forEach() 19 | 13.JavaScript from() 20 | 14.JavaScript includes() 21 | 15.JavaScript indexOf() 22 | 16.JavaScript isArray() 23 | 17.JvaScript join() 24 | 18.JvaScript keys() 25 | 19.JavaScript lastIndexOf() 26 | 20.JavaScript length() 27 | 21.JavaScript map() 28 | 22.JavaScript of() 29 | 23.JavaScript pop() 30 | 24.JvaScript push() 31 | 25.JvaScript reduce() 32 | 26.JvaScript reduceRight() 33 | 27.JvaScript reverse() 34 | 28.JavaScript shift() 35 | 29.JavaScript slice() 36 | 30.JavaScript some() 37 | 31.JavaScript sort() 38 | 32.JavaScript splice() 39 | 33.JavaScript toLocaleString() 40 | 34.JavaScript toString() 41 | 35.JavaScript unshift() 42 | 36.JavaScript values() 43 | */ -------------------------------------------------------------------------------- /6. Types in JavaScript/14.js: -------------------------------------------------------------------------------- 1 | // Array Concat() 2 | 3 | const arr1 = [1, 2, 3, 4] 4 | const arr2 = [2, 4, 6, 8] 5 | 6 | console.log(arr1.concat(arr2)); 7 | 8 | 9 | // Or 10 | 11 | const arr3 = arr1.concat(arr2) 12 | console.log(arr3); 13 | 14 | 15 | 16 | // Or 17 | // Adding in Middile of it 18 | const arr4 = arr1.concat("I am String", arr2) 19 | console.log(arr4); 20 | 21 | // Concatenating Multiple Arrays 22 | const arr5 = arr1.concat(arr2, arr3, arr4) 23 | console.log(arr5); 24 | 25 | 26 | -------------------------------------------------------------------------------- /6. Types in JavaScript/15.js: -------------------------------------------------------------------------------- 1 | // constructor() 2 | 3 | 4 | 5 | let languages = ["JavaScript", "Java", "Python"]; 6 | 7 | let constructor = languages.constructor; 8 | console.log(constructor) -------------------------------------------------------------------------------- /6. Types in JavaScript/16.js: -------------------------------------------------------------------------------- 1 | const even = [2, 4, 6, 8] 2 | 3 | let mix = even.copyWithin(0, 3) 4 | console.log(mix);// 0 chnage to 3 in index 5 | // 2 chnages to 8 6 | // 8 4 6 8 7 | 8 | 9 | const odd = [1, 3, 5, 7] 10 | let mix2 = odd.copyWithin(-1) 11 | console.log(mix2); 12 | // -1 will copy the 0th index to last one index 13 | 14 | -------------------------------------------------------------------------------- /6. Types in JavaScript/17.js: -------------------------------------------------------------------------------- 1 | const even = [2, 4, 6, 8] 2 | let hey = even.entries() 3 | 4 | console.log(hey); // Array Iterator 5 | console.log(hey.next().value); 6 | console.log(hey.next().value); 7 | console.log(hey.next().value); 8 | console.log(hey.next().value); 9 | 10 | 11 | /* 12 | [ 0, 2 ] 13 | [ 1, 4 ] 14 | [ 2, 6 ] 15 | [ 3, 8 ] 16 | 17 | */ 18 | 19 | for (let entry of hey) { 20 | console.log(entry); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /6. Types in JavaScript/18.js: -------------------------------------------------------------------------------- 1 | // Array Every() 2 | 3 | const arr = [ 4 | { 5 | name: "Luke Skywalker", 6 | height: 172, 7 | mass: 77, 8 | eye_color: "blue", 9 | gender: "male", 10 | }, 11 | { 12 | name: "Darth Vader", 13 | height: 202, 14 | mass: 136, 15 | eye_color: "yellow", 16 | gender: "male", 17 | }, 18 | { 19 | name: "Leia Organa", 20 | height: 150, 21 | mass: 49, 22 | eye_color: "brown", 23 | gender: "male", 24 | }, 25 | { 26 | name: "Anakin Skywalker", 27 | height: 188, 28 | mass: 84, 29 | eye_color: "blue", 30 | gender: "male", 31 | }, 32 | ]; 33 | 34 | for (let i of arr) { 35 | if (i.eye_color == "blue") { 36 | console.log(i.eye_color, i.name); 37 | } 38 | } 39 | 40 | for (let j = 0; j <= arr.length - 1; j++) { 41 | if (arr[j].eye_color == "blue") { 42 | console.log(arr[j].eye_color, arr[j].name); 43 | } 44 | } 45 | 46 | const masss = arr.every((e) => e.mass > 10); 47 | 48 | console.log(masss); 49 | 50 | const heights = arr.every((e) => e.height <= 202); 51 | console.log(heights); 52 | 53 | const males = arr.every((e) => e.gender == "male"); 54 | console.log(males); 55 | -------------------------------------------------------------------------------- /6. Types in JavaScript/19.js: -------------------------------------------------------------------------------- 1 | // Array Fill() 2 | 3 | const arr = [1, 2, 3, 4]; 4 | console.log(arr); 5 | 6 | arr.fill(10); 7 | console.log(arr); 8 | 9 | const arr2 = ["Suhail", "Roushan", "Ali"]; 10 | console.log(arr2); 11 | 12 | arr2.fill("Hello World"); 13 | console.log(arr2); 14 | 15 | const arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 16 | console.log(arr3); 17 | arr3.fill(1, -2); 18 | console.log(arr3); 19 | 20 | -------------------------------------------------------------------------------- /6. Types in JavaScript/2.js: -------------------------------------------------------------------------------- 1 | /* 2 | Working of JavaScript Arrays 3 | In JavaScript, an array is an object. And, the indices of arrays are objects keys. 4 | 5 | Since arrays are objects, the array elements are stored by reference. Hence, when an array value is copied, any change in the copied array will also reflect in the original array. For example, 6 | 7 | let arr = ['h', 'e']; 8 | let arr1 = arr; 9 | arr1.push('l'); 10 | 11 | console.log(arr); // ["h", "e", "l"] 12 | console.log(arr1); // ["h", "e", "l"] 13 | Run Code 14 | You can also store values by passing a named key in an array. For example, 15 | 16 | let arr = ['h', 'e']; 17 | arr.name = 'John'; 18 | 19 | console.log(arr); // ["h", "e"] 20 | console.log(arr.name); // "John" 21 | console.log(arr['name']); // "John" 22 | Run Code 23 | Array indexing in JavaScript 24 | Array indexing in JavaScript 25 | However, it is not recommended to store values by passing arbitrary names in an array. 26 | 27 | Hence in JavaScript, you should use an array if values are in ordered collection. Otherwise it's better to use object with { }. 28 | 29 | 30 | 31 | 32 | 33 | 34 | */ 35 | 36 | -------------------------------------------------------------------------------- /6. Types in JavaScript/20.js: -------------------------------------------------------------------------------- 1 | // Array Filter() 2 | 3 | const arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10]; 4 | // console.log(arr); 5 | // arr.filter((e) => { 6 | // if (e % 2 == 0) { 7 | // console.log(e); 8 | // } 9 | // }); 10 | 11 | const isEven = (num) => { 12 | if (num % 2 == 0) { 13 | return num; 14 | } 15 | }; 16 | 17 | let news = arr.filter(isEven); 18 | // console.log(news); 19 | 20 | const words = [ 21 | "Suhail", 22 | "Roushan", 23 | "Apple", 24 | "Mango", 25 | "Android", 26 | "Samsung", 27 | "Ronak", 28 | "Man", 29 | "Sun", 30 | "Suzuki", 31 | ]; 32 | function search(num) { 33 | for (let i of words) { 34 | // console.log(i); 35 | let arr = []; 36 | arr.push(i); 37 | if (arr[0][0] == num[0]) { 38 | console.log(arr.join("")); 39 | } 40 | } 41 | } 42 | // search("A"); 43 | 44 | const prices = [1800, 2000, null, 3000, 5000, "Thousand", 500, 8000]; 45 | 46 | let ushar = prices.filter((e) => { 47 | if (e > 2000 && e != null) { 48 | console.log(e); 49 | } 50 | }); 51 | -------------------------------------------------------------------------------- /6. Types in JavaScript/21.js: -------------------------------------------------------------------------------- 1 | // Array Find () 2 | // It Only Returns the First Values from Array 3 | // const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 4 | 5 | // const evenfind = arr.find((num) => num % 2 == 0); 6 | // console.log(evenfind); 7 | 8 | // // Array filter filters the whole array 9 | // const even = arr.filter((num) => !!(num & 1)); 10 | 11 | // console.log(even); 12 | 13 | const team = [ 14 | { name: "Bill", age: 10 }, 15 | { name: "Linus", age: 15 }, 16 | { name: "Alan", age: 20 }, 17 | { name: "Steve", age: 34 }, 18 | ]; 19 | 20 | let find = team.find((e) => e.age > 10); 21 | console.log(find); // Linus 22 | 23 | let find1 = team.filter((e) => e.age > 10); 24 | console.log(find1); 25 | // All Greater than 10 26 | // [ 27 | // { name: "Linus", age: 15 }, 28 | // { name: "Alan", age: 20 }, 29 | // { name: "Steve", age: 34 }, 30 | // ]; 31 | -------------------------------------------------------------------------------- /6. Types in JavaScript/22.js: -------------------------------------------------------------------------------- 1 | // Find Ki Index (Only First Element ka Index) 2 | // The findIndex() method returns the index of the 3 | // first array element that satisfies the provided 4 | // test function or else returns -1. 5 | 6 | // If the element does not match it will send -1 7 | const arr = [2, 8, 10, 31, 4]; 8 | 9 | let ok = arr.findIndex((e) => e % 2 != 0); 10 | console.log(ok); 11 | 12 | let days = ["Sunday", "Wednesday", "Tuesday", "Friday"]; 13 | 14 | let ok2 = days.findIndex((e) => e == "Wed"); 15 | console.log(ok2); 16 | -------------------------------------------------------------------------------- /6. Types in JavaScript/23.js: -------------------------------------------------------------------------------- 1 | // Array Flat () 2 | 3 | const arr = [1, 2, [3, 4], [5, 6], [7, 8]]; 4 | // 1 2 3 5 | let ok = arr.flat(); // Removes all nested arrays 6 | // console.log(ok); 7 | 8 | const arr1 = [1, 2, [3, [4, 5]]]; 9 | let ok1 = arr1.flat(1); // Removes 1 Array [] Literal 10 | console.log(ok1); 11 | 12 | const arr2 = [1, 2, [3, [4, 5, [6, 7]]]]; 13 | let ok2 = arr2.flat(2); // Removes 2 Array [] Literal 14 | console.log(ok2); 15 | 16 | const arr3 = [1, 2, [3, [4, 5, [6, 7, [8, 9]]]]]; 17 | let ok3 = arr3.flat(3); // Removes 3 Array [] Literal 18 | console.log(ok3); 19 | 20 | const holes = [1, 2, 3, , 4, , 5, ,]; 21 | console.log(holes.flat()); // Removed all empty values 22 | 23 | const hey = [1, 2, [3, 4, [5, 6]]]; 24 | console.log(hey.flat(2)); 25 | -------------------------------------------------------------------------------- /6. Types in JavaScript/24.js: -------------------------------------------------------------------------------- 1 | // Array FlatMap () 2 | 3 | // The flatMap() method first maps each element of an 4 | // array using a mapping function, 5 | // then flattens it into a new array. 6 | 7 | const arr = [1, 2, 3, 4, 5]; 8 | 9 | let search = arr.flatMap((e) => e ** 2); 10 | console.log(search); 11 | 12 | 13 | /* 14 | flatMap() Return Value 15 | Returns a new array after mapping every element using callback 16 | Notes: 17 | 18 | The flatMap() method does not change the original array. 19 | The flatMap() method is equivalent to array.map().flat(). 20 | 21 | 22 | 23 | */ 24 | 25 | // defining an array 26 | let numbers = [1, 2, 3, 4, 5]; 27 | 28 | // incrementing each element of array using map() 29 | let afterMapping = numbers.map((element) => element + 2); 30 | 31 | // flattening the array using flat() 32 | console.log(afterMapping); 33 | 34 | let afterFlattening = afterMapping.flat(); 35 | 36 | console.log(afterFlattening); // [ 3, 4, 5, 6, 7 ] 37 | 38 | // using flatMap() instead of flat() and map() 39 | let after_flatMap = numbers.flatMap((element) => element + 2); 40 | 41 | console.log(after_flatMap); // [ 3, 4, 5, 6, 7 ] 42 | 43 | -------------------------------------------------------------------------------- /6. Types in JavaScript/25.js: -------------------------------------------------------------------------------- 1 | // ForEach () 2 | 3 | const arr = [ 4 | "Java", 5 | "JavaScript", 6 | "Python", 7 | "C", 8 | "C++", 9 | "Kotlin", 10 | "Dart", 11 | "Bash", 12 | "C#", 13 | "Swift", 14 | "GO", 15 | "R", 16 | "PHP", 17 | ]; 18 | 19 | console.log(arr); 20 | console.log("Using For Loop"); 21 | 22 | for (let i = 0; i <= arr.length - 1; i++) { 23 | console.log(arr[i]); 24 | } 25 | console.log("Using For Each"); 26 | arr.forEach((i, index) => { 27 | console.log(index + 1, i); 28 | }); 29 | 30 | const arr1 = [10, 20, 30, 40, 50]; 31 | arr1.forEach((i, index, arr) => { 32 | console.log(index + 1, i); 33 | }); 34 | -------------------------------------------------------------------------------- /6. Types in JavaScript/26.js: -------------------------------------------------------------------------------- 1 | // Array.from() 2 | 3 | // The from() method creates a new array from any array-like or iterable object. 4 | 5 | // creating a new array from string 6 | 7 | // Uses of Array.from () 8 | /* 9 | 1.String to Array "12142" ==> ["1","2","1","4","2"] 10 | 11 | The from() method creates a new array from any array-like or iterable object. 12 | Parameters: This method accepts three parameters as mentioned above and described below: 13 | 14 | object: This parameter holds that object that will convert into an array 15 | 16 | mapFunction: This parameter is optional used to call on each item of the array. 17 | 18 | thisValue: This parameter is optional, it holds the context to be passed as this to 19 | be used while executing the mapFunction. If the context is passed, it will be used 20 | like this for each invocation of the callback function, otherwise undefined is used as default. 21 | 22 | Return value: It returns a new Array instance whose elements are same as the given array. 23 | In the case of a string, every alphabet of the string is converted to an element of the new array 24 | instance. Below example illustrate the Array from() method in JavaScript: 25 | 26 | 27 | Example 28 | */ 29 | const arr = "12142"; 30 | console.log(Array.from(arr, Number)); // [ '1', '2', '1', '4', '2' ] 31 | 32 | // 2nd Parameter takes map 33 | // console.log(Array.from(arr, (x) => Number(x))); 34 | -------------------------------------------------------------------------------- /6. Types in JavaScript/27.js: -------------------------------------------------------------------------------- 1 | // Array Includes() 2 | 3 | // Array Includes value in true or false 4 | // The includes() method returns: 5 | 6 | // true if searchValue is found anywhere within the array 7 | // false if searchValue is not found anywhere within the array 8 | 9 | // Array Includes check the given element is 10 | // present in the array or not 11 | // 12 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 13 | 14 | console.log(arr.includes(11)); // false 15 | console.log(arr.includes(4)); // true 16 | // 11 is not in the array so it gave false 17 | // 4 is in the array so it gave true 18 | 19 | // The includes() method is case sensitive. For example: 20 | 21 | const words = ["Java", "Python", "C"]; 22 | console.log(words.includes("c")); // false 23 | // There is no lowerCase c in the array 24 | console.log(words.includes("C")); // true 25 | // There is UpperCase C in the array 26 | 27 | 28 | // 2nd Parameter is for to check from a specific index of array 29 | const test = [0, 1, 2, 3, 4, 5, 6, 7, 8]; 30 | console.log(test.includes(4, 5)); // Checking 4 from 5th index // false 31 | console.log(test.includes(4, 1)); // Checking 4 from 1st index // true 32 | 33 | 34 | -------------------------------------------------------------------------------- /6. Types in JavaScript/28.js: -------------------------------------------------------------------------------- 1 | var priceList = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 2 | 3 | function find(arr, num) { 4 | arr.includes(num) ? console.log("Hau") : console.log("No"); 5 | } 6 | find(priceList, 1); 7 | -------------------------------------------------------------------------------- /6. Types in JavaScript/29.js: -------------------------------------------------------------------------------- 1 | var priceList = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 2 | 3 | function find(arr, num) { 4 | if (arr.includes(num)) { 5 | console.log("Hau Hai"); 6 | } else { 7 | console.log("No Bro"); 8 | } 9 | } 10 | find(priceList, 19); 11 | -------------------------------------------------------------------------------- /6. Types in JavaScript/3.js: -------------------------------------------------------------------------------- 1 | // Array can be Declaring Using 2 Ways 2 | // 1.Array Literals 3 | const arr1 = [1, 2, 3, 4, 5, 6] 4 | // 2. Using New Array 5 | const arr2 = new Array(1, 2, 3, 4, 5, 6) 6 | 7 | // To Check if the given variable is array or not 8 | // 1. Using Array.isArray() 9 | 10 | console.log(Array.isArray(arr1)); // true 11 | 12 | const arr3 = [{}] 13 | console.log(Array.isArray(arr3)); // true 14 | 15 | const arr4 = {} 16 | console.log(Array.isArray(arr4)); // false 17 | 18 | 19 | // Comparing 2 same values arrays 20 | 21 | console.log(arr1 == arr2); // Even though values are same but differnt way to create array 22 | 23 | const arr5 = [1, 2, 3, 4, 5, 6] 24 | 25 | console.log(arr1 == arr5); // Even though values are same and same way to create array but both are not equal to it 26 | 27 | 28 | // Adding a element at first postion using ==> unshift() 29 | 30 | const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 31 | numbers.unshift(0) 32 | console.log(numbers); //[0, 1, 2, 3, 4,5, 6, 7, 8, 9,10] 33 | 34 | // Adding a element a last postition using ==> push() 35 | 36 | numbers.push(11) 37 | console.log(numbers); //[0, 1, 2, 3, 4,5, 6, 7, 8, 9,10,11] 38 | 39 | 40 | // Removing a element from first position using shift() 41 | numbers.shift() 42 | console.log(numbers); //[1, 2, 3, 4,5, 6, 7, 8, 9,10,11] 43 | 44 | // Removing a element from last poisiting using pop() 45 | 46 | numbers.pop() 47 | console.log(numbers);//[1, 2, 3, 4,5, 6, 7, 8, 9,10] 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /6. Types in JavaScript/30.js: -------------------------------------------------------------------------------- 1 | // Indexof Array Method 2 | 3 | // Array Indexof gives the index of the specifc element from 4 | // the array 5 | 6 | // IF THERE IS NO INDEX OF THAT ELEMENT IF GIVE RETURN -1 7 | 8 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 9 | 10 | console.log(arr.indexOf(1)); // 0 11 | console.log(arr.indexOf(2)); // 1 12 | console.log(arr.indexOf(10)); // 9 13 | 14 | const words = ["Java", "C", "C++", "Python"]; 15 | // Indexs are 0 1 2 3 16 | 17 | console.log(words.indexOf("Java")); // 0 18 | console.log(words.indexOf("C++")); 2 19 | console.log(words.indexOf("JavaScript")); // -1 20 | -------------------------------------------------------------------------------- /6. Types in JavaScript/31.js: -------------------------------------------------------------------------------- 1 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 2 | 3 | const search = (arr, num) => { 4 | if (arr.indexOf(num) == -1) { 5 | console.log("Unavaliable"); 6 | } else { 7 | console.log("Hau hai re bhai"); 8 | } 9 | }; 10 | 11 | search(arr,10) -------------------------------------------------------------------------------- /6. Types in JavaScript/32.js: -------------------------------------------------------------------------------- 1 | // Array is.Array() // 2 | // Checks the array is a array or not 3 | 4 | // Returns true if the array is true 5 | // Returns false if the array is false 6 | 7 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 8 | 9 | console.log(arr); 10 | 11 | console.log(Array.isArray(arr)); 12 | 13 | // 14 | 15 | const arr2 = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 16 | console.log(arr2); 17 | 18 | console.log(Array.isArray(arr2)); 19 | 20 | console.log(typeof arr2); // Its an Object but shows as Array 21 | 22 | -------------------------------------------------------------------------------- /6. Types in JavaScript/33.js: -------------------------------------------------------------------------------- 1 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 2 | 3 | const arr2 = arr; 4 | 5 | console.log(arr2 === arr); 6 | 7 | const s = "Hello"; 8 | const j = s; 9 | console.log(j === s); 10 | 11 | const a = { name: "Suhail" }; 12 | const b = a; 13 | console.log(a === b); 14 | 15 | const arr3 = new Array(1, 2, 3, 4, 4, 5); 16 | console.log((arr == arr3)); 17 | console.log(typeof arr); 18 | console.log(typeof arr3); 19 | 20 | 21 | -------------------------------------------------------------------------------- /6. Types in JavaScript/34.js: -------------------------------------------------------------------------------- 1 | // passing an empty array [] 2 | console.log(Array.isArray([])); // true 3 | 4 | // we have created an array with element 7 and 5 | // passed that value to isArray() 6 | console.log(Array.isArray(new Array(7))); // true 7 | 8 | // passing a boolean value 9 | console.log(Array.isArray(true)); // false 10 | 11 | // passing undefined 12 | console.log(Array.isArray(undefined)); // false 13 | 14 | // not passing any argument in isArray() 15 | console.log(Array.isArray()); // false 16 | -------------------------------------------------------------------------------- /6. Types in JavaScript/35.js: -------------------------------------------------------------------------------- 1 | // Array Join() Method 2 | // The join() method returns a new string by 3 | // concatenating all of the elements in an array, 4 | // separated by a specified separator. 5 | 6 | /* 7 | The join() method does not change the original array. 8 | Elements like undefined, null, or empty array have an 9 | empty string representation. 10 | */ 11 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 12 | 13 | console.log(arr.join()); 14 | // 1,2,3,4,5,6,7,8,9,10 15 | 16 | console.log(arr.join("")); 17 | // 12345678910 18 | 19 | console.log(arr.join(" | ")); 20 | // 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; 21 | 22 | const arr2 = ["Java", "C", "C++"]; 23 | console.log(arr2.join(" ")); 24 | // Java C C++ 25 | 26 | console.log(arr.join("+")); 27 | // 1+2+3+4+5+6+7+8+9+10 28 | -------------------------------------------------------------------------------- /6. Types in JavaScript/36.js: -------------------------------------------------------------------------------- 1 | // JavaScript Array keys() 2 | 3 | // The keys() method returns a new Array 4 | // Iterator object that contains the keys 5 | // for each element in the array. 6 | 7 | const arr = ["Java", "C", "C++", "Python", "Javascript"]; 8 | 9 | let store = arr.keys(); 10 | 11 | // for (let i of store) { 12 | // console.log(i); 13 | // } 14 | // It Also gives keys for empty array to 15 | const arr2 = [ 16 | { name: "Rahul", score: 89 }, // 0 17 | { name: "Vivek", score: 88 }, // 1 18 | , // 2 19 | { name: "Sourav", score: 82 },// 3 20 | { name: "Gautam", score: 91 },// 4 21 | { name: "Sunil", score: 79 }, // 5 22 | ]; 23 | 24 | let store2 = arr2.keys(); 25 | 26 | for (let j of store2) { 27 | console.log(j); 28 | } 29 | /* 30 | 0 31 | 1 32 | 2 33 | 3 34 | 4 35 | 5 36 | */ -------------------------------------------------------------------------------- /6. Types in JavaScript/37.js: -------------------------------------------------------------------------------- 1 | // Array lastindexof() 2 | // The lastIndexOf() method returns the index of the last 3 | // occurrence of a specified element in the array. 4 | 5 | // Example; 6 | const arr = ["s", "u", "h", "a", "i", "l", "r", "o", "n", "s", "h", "a", "n"]; 7 | 8 | // Couting Index starts from 1 to 12 from left to right 9 | // But Finding element starts from right to left 10 | console.log(arr.lastIndexOf("n")); //12 11 | console.log(arr.lastIndexOf("a")); // 11 12 | 13 | // It accepts 2 parameters 14 | 15 | // arr.lastindexof(ele,startfromindex) 16 | // fromIndex starts from 1 17 | 18 | 19 | -------------------------------------------------------------------------------- /6. Types in JavaScript/38.js: -------------------------------------------------------------------------------- 1 | // Array Length() 2 | // .length gives the count of array which starts from 1 3 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 4 | console.log(arr.length); //10; 5 | 6 | arr.length = 4; 7 | console.log(arr); // [ 1, 2, 3, 4 ] 8 | arr.length = 8; 9 | console.log(arr); // [ 1, 2, 3, 4, <4 empty items> ] 10 | 11 | const arr2 = []; 12 | console.log(arr2.length); 13 | 14 | var companyList = ["Apple", "Google", "Facebook", "Amazon"]; 15 | console.log(companyList.length); // Output: 4 16 | 17 | var randomList = ["JavaScript", 44]; 18 | console.log(randomList.length); // Output: 2 19 | 20 | var emptyArray = []; 21 | console.log(emptyArray.length); // Output: 0 22 | 23 | // Example 2: Using Array length in for loop 24 | var languages = ["JavaScript", "Python", "C++", "Java", "Lua"]; 25 | 26 | // languages.length can be used to find out 27 | // the number of times to loop over an array 28 | for (let i = 0; i < languages.length; i++) { 29 | console.log(languages[i]); 30 | } 31 | 32 | // Example 3: Changing length property of Array 33 | var languages = ["JavaScript", "Python", "C++", "Java", "Lua"]; 34 | 35 | // truncate the Array to 3 elements 36 | languages.length = 3; 37 | 38 | // Output: [ 'JavaScript', 'Python', 'C++' ] 39 | console.log(languages); 40 | 41 | // extend the Array to length 6 42 | languages.length = 6; 43 | 44 | // Output: [ 'JavaScript', 'Python', 'C++', <3 empty items> ] 45 | console.log(languages); 46 | -------------------------------------------------------------------------------- /6. Types in JavaScript/4.js: -------------------------------------------------------------------------------- 1 | // Updating an Array Value 2 | 3 | const arr = [1, 2] 4 | console.log(arr);// 1,2 5 | 6 | console.log(arr[0]); //1 7 | console.log(arr[1]); //2 8 | 9 | arr[1] = 3 10 | // Value got updated and now its 1,3 11 | console.log(arr[1]); // 3 12 | 13 | // 14 | arr[5] = 50; 15 | console.log(arr); 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /6. Types in JavaScript/40.js: -------------------------------------------------------------------------------- 1 | // for in Loop 2 | // Use for Objects only ...Dont Use In Arrays (if index is matter for you) 3 | 4 | let data = { 5 | name: "Suhail", 6 | age: 20, 7 | isAlive: true, 8 | favnum: [10, 20, 11], 9 | address: { 10 | city: "Hyderabad", 11 | }, 12 | }; 13 | 14 | for (let i in data) { 15 | console.log(`Key is this ${i} : ${data[i]} Value`); 16 | } 17 | -------------------------------------------------------------------------------- /6. Types in JavaScript/41.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/41.js -------------------------------------------------------------------------------- /6. Types in JavaScript/42.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/42.js -------------------------------------------------------------------------------- /6. Types in JavaScript/43.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/43.js -------------------------------------------------------------------------------- /6. Types in JavaScript/44.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/44.js -------------------------------------------------------------------------------- /6. Types in JavaScript/45.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/45.js -------------------------------------------------------------------------------- /6. Types in JavaScript/46.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/46.js -------------------------------------------------------------------------------- /6. Types in JavaScript/47.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/47.js -------------------------------------------------------------------------------- /6. Types in JavaScript/48.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/48.js -------------------------------------------------------------------------------- /6. Types in JavaScript/49.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/6. Types in JavaScript/49.js -------------------------------------------------------------------------------- /6. Types in JavaScript/5.js: -------------------------------------------------------------------------------- 1 | // Using name value pair in array 2 | const arr1 = [1, 2, 3] 3 | arr1.name = "Hello" 4 | arr1.firstname = 'Suhail' 5 | arr1.lastname = 'Roushan' 6 | arr1.age = 20 7 | arr1.ok = ['h1', 'h3'] 8 | console.log(arr1[0]); 9 | console.log(arr1[1]); 10 | console.log(arr1[2]); 11 | console.log(arr1[3]); 12 | console.log(arr1[4]); 13 | 14 | console.log(arr1); 15 | console.log(arr1.length); 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /6. Types in JavaScript/6.js: -------------------------------------------------------------------------------- 1 | // Access elements from an Array 2 | const arr = [1, "two", 3, true, null, Symbol(3), BigInt(45), {}] 3 | for (key of arr) { 4 | 5 | console.log(key); 6 | } 7 | 8 | 9 | for (let i = 0; i <= arr.length - 1; i++) { 10 | console.log(arr[i]); 11 | 12 | } -------------------------------------------------------------------------------- /6. Types in JavaScript/7.js: -------------------------------------------------------------------------------- 1 | let first = "Suhail"; 2 | console.log(first); 3 | 4 | 5 | let second = first 6 | console.log(first); 7 | 8 | first = "Roushan" 9 | console.log(second); 10 | 11 | 12 | let obj1 = { 13 | name: "Suhail", 14 | age: 20 15 | } 16 | console.log(obj1); 17 | 18 | let obj2 = obj1 19 | console.log(obj2); 20 | 21 | obj1.isAlive = true; 22 | console.log(obj2); 23 | console.log(obj1); 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /6. Types in JavaScript/8.js: -------------------------------------------------------------------------------- 1 | // Value Types Data Types are Primitive Data Types ==> Immutable 2 | // String , Symbol , Boolean , BigInt , null , Number , undefined 3 | // Stores Data in Stack in Last in First Out Order LIFO 4 | // Its has less space than compared to other data structres 5 | let suhail = 1234; 6 | console.log(suhail); // 1234 7 | 8 | let ayeen = suhail; 9 | console.log(ayeen); // 1234 10 | 11 | suhail = 4567; 12 | 13 | console.log(ayeen); // 1234 14 | 15 | // Reference Types Data Types are Non Primitive Data Types ==> Mutable 16 | // Function , Arrays , Dates , Regex , Objects 17 | // Stores Data in Heap 18 | // Its is slower than stack 19 | // But much usefull for complex variables 20 | let obj1 = { name: "Suhail", age: 20 } 21 | console.log(obj1); // { name: 'Suhail', age: 20 } 22 | 23 | let obj2 = obj1; 24 | console.log(obj2); // { name: 'Suhail', age: 20 } 25 | 26 | obj1.name = "Roushan" 27 | 28 | console.log(obj2); // { name: 'Roushan', age: 20 } 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /6. Types in JavaScript/9.js: -------------------------------------------------------------------------------- 1 | // To Empty an Array 2 | 3 | // Declaring from var and let 4 | console.log("Using Var"); 5 | 6 | var arr1 = [1, 2, 3, 4] 7 | console.log(arr1); // 1 2 3 4 8 | 9 | // Making it Empty 10 | 11 | arr1 = []; 12 | console.log(arr1); // Empty 13 | 14 | console.log("Using Let"); 15 | 16 | let arr2 = [5, 6, 7, 8] 17 | console.log(arr2); // 5 6 7 8 18 | 19 | // Making it Empty 20 | 21 | arr2 = []; 22 | console.log(arr2); // Empty 23 | 24 | 25 | // Declaring Const 26 | // In Const you cannot you reassign and redeclare 27 | 28 | // It means you cannot do arr = [] 29 | console.log("Using Cont"); 30 | 31 | const arr3 = [11, 12, 13, 14] 32 | console.log(arr3); // 11 12 13 14 33 | console.log(arr3.length); 34 | 35 | // Making it empty by .length = 0 36 | 37 | arr3.length = 0; 38 | console.log(arr3); // Empty 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /6. Types in JavaScript/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "6.-types-in-javascript", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "1.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC" 13 | } 14 | -------------------------------------------------------------------------------- /6. Types in JavaScript/worksheet.js: -------------------------------------------------------------------------------- 1 | const characters = [ 2 | { 3 | name: 'Luke Skywalker', 4 | height: 172, 5 | mass: 77, 6 | eye_color: 'blue', 7 | gender: 'male', 8 | }, 9 | { 10 | name: 'Darth Vader', 11 | height: 202, 12 | mass: 136, 13 | eye_color: 'yellow', 14 | gender: 'male', 15 | }, 16 | { 17 | name: 'Leia Organa', 18 | height: 150, 19 | mass: 49, 20 | eye_color: 'brown', 21 | gender: 'female', 22 | }, 23 | { 24 | name: 'Anakin Skywalker', 25 | height: 188, 26 | mass: 84, 27 | eye_color: 'blue', 28 | gender: 'male', 29 | }, 30 | ]; 31 | 32 | //***MAP*** 33 | //1. Get array of all names 34 | //2. Get array of all heights 35 | //3. Get array of objects with just name and height properties 36 | //4. Get array of all first names 37 | 38 | //***REDUCE*** 39 | //1. Get total mass of all characters 40 | //2. Get total height of all characters 41 | //3. Get total number of characters by eye color 42 | //4. Get total number of characters in all the character names 43 | 44 | //***FILTER*** 45 | //1. Get characters with mass greater than 100 46 | //2. Get characters with height less than 200 47 | //3. Get all male characters 48 | //4. Get all female characters 49 | 50 | //***SORT*** 51 | //1. Sort by mass 52 | //2. Sort by height 53 | //3. Sort by name 54 | //4. Sort by gender 55 | 56 | //***EVERY*** 57 | //1. Does every character have blue eyes? 58 | //2. Does every character have mass more than 40? 59 | //3. Is every character shorter than 200? 60 | //4. Is every character male? 61 | 62 | //***SOME*** 63 | //1. Is there at least one male character? 64 | //2. Is there at least one character with blue eyes? 65 | //3. Is there at least one character taller than 210? 66 | //4. Is there at least one character that has mass less than 50? -------------------------------------------------------------------------------- /7. Array-Methods/1. at().js: -------------------------------------------------------------------------------- 1 | // Array At() Method 2 | // It accepts the index number and gives the element 3 | // of that specific index 4 | const arr = ["Java", "C", "C++", "Python", "Javascript"]; 5 | 6 | console.log(arr.at(0)); 7 | // Index ==> Element (Java) 8 | console.log(arr.at(1)); 9 | console.log(arr.at(2)); 10 | console.log(arr.at(3)); 11 | console.log(arr.at(4)); 12 | console.log(arr.at(5)); // if there is no index 13 | // then it will show undefined 14 | 15 | // Index ===> element 16 | 17 | // Note: It only returns a element of a specific index 18 | -------------------------------------------------------------------------------- /7. Array-Methods/10. findIndex().js: -------------------------------------------------------------------------------- 1 | // Array FindIndex() 2 | // Array FindIndex Method Returns the First Element Index from ther 3 | // Array Which passes the test given in the defined in 4 | // the function 5 | 6 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 7 | 8 | console.log(arr.findIndex((x) => x % 2 == 0)); // 2 index is 1 9 | 10 | // 2 is the first element from finding all even numbers and index 1 11 | -------------------------------------------------------------------------------- /7. Array-Methods/11. findLastIndex().js: -------------------------------------------------------------------------------- 1 | // Array findLastIndex() 2 | // Depcrated Not In Work 3 | // that is no longer supported or maintained 4 | -------------------------------------------------------------------------------- /7. Array-Methods/12. flat().js: -------------------------------------------------------------------------------- 1 | // Array flat() 2 | // Array flat Methods returns a new array by 3 | // converting all multidimensional array into 4 | // single nested array or 1d array 5 | // Example :- 6 | // [1,2,[3,5],[3,7],4] ===> [1,2,3,5,3,7,4] 7 | // The flat() method creates a new array by 8 | // flattening a nested array up to the specified depth. 9 | 10 | let numbers = [1, 2, [3, 4, [5, 6, [7, 8]]]]; 11 | 12 | // reducing nesting by flattening the array to depth 2 13 | let store = numbers.flat(3); 14 | console.log(store); 15 | 16 | // Flat is also used to remove holes from an array 17 | 18 | // Example :- [1,,3,,5] ==> [1,3,5] 19 | 20 | const arr2 = [1, , 5, 7, 33, , 5]; 21 | console.log(arr2.flat()); 22 | // [ 1, 5, 7, 33, 5 ] 23 | -------------------------------------------------------------------------------- /7. Array-Methods/13. flatMap().js: -------------------------------------------------------------------------------- 1 | // Array flatMap() 2 | // The flatMap() method first maps each 3 | // element of an array using a mapping function, 4 | // then flattens it into a new array. 5 | const arr = [1, 2, 3, 4, 5, 6, , 8, 9, 10]; 6 | 7 | console.log(arr.flatMap((x) => x ** 2)); 8 | // [ 9 | // 1, 4, 9, 16, 25, 10 | // 36, 64, 81, 100 11 | // ] 12 | 13 | const arr2 = [1, 2, 34, 5, 6, 67, 7, [23, 4, 5, 5, [, 4, 5, 6, 6]]]; 14 | console.log(arr2.flat(2).flatMap((x) => x ** 2)); 15 | // [1, 4, 1156, 25, 36, 4489, 49, 529, 16, 25, 25, 16, 25, 36, 36] 16 | -------------------------------------------------------------------------------- /7. Array-Methods/14. forEach().js: -------------------------------------------------------------------------------- 1 | // Array forEach() Method 2 | 3 | /* 4 | The forEach() method calls a function for each element in an array. 5 | The forEach() method is not executed for empty elements. 6 | 7 | Always remember that it doesn’t return anything 8 | and if you try to get the value it will be undefined. 9 | 10 | */ 11 | 12 | // Using ForEach 13 | // 1. The returning value 14 | // Using ForEach 15 | 16 | let numbers = [5, 4, 3, 2, 1]; 17 | // It uses for Displaying 18 | // not for Returning Array 19 | numbers.forEach((x) => console.log(x)); // 5 4 3 2 1 20 | // It does not change the Original Array 21 | 22 | // For Returning 23 | console.log(numbers.forEach((x) => x)); // undefined 24 | let data = numbers.forEach((x) => x); 25 | console.log(data); // undefined 26 | 27 | // 2. Ability to chain other methods 28 | 29 | // Using ForEach 30 | // As it returns undefiend you cannot change to other array methods 31 | // console.log(numbers.forEach((x) => x * x).fitter((x) => x > 9)); // undefined 32 | 33 | // 3. Mutability 34 | 35 | //Mutability means 36 | // Capacity or ability to change." 37 | //A mutable object is an object whose state can be changed after its creation. 38 | // Regarding mutability, 39 | // how do the foreach( ) and map( ) methods in Javascript behave 40 | // Using For Fach 41 | //forEach() does not modify the array in which it is called,this).( However ,the callback method can do 42 | 43 | console.log(numbers); 44 | numbers.forEach((ele, index) => console.log((numbers[index] = ele * ele))); 45 | console.log(numbers); 46 | /* 47 | [ 5, 4, 3, 2, 1 ] 48 | 25 49 | 16 50 | 9 51 | 4 52 | 1 53 | [ 25, 16, 9, 4, 1 ] 54 | */ 55 | 56 | 57 | 58 | //4. Performance Speed 59 | const myAwesomeArray = [1, 2, 3, 4, 5]; 60 | const start = performance.now(); 61 | myAwesomeArray.forEach((x) => x + x * 10000000000); 62 | const end = performance.now(); 63 | console.log(end - start); 64 | // 0.08336000144481659 65 | -------------------------------------------------------------------------------- /7. Array-Methods/15. from().js: -------------------------------------------------------------------------------- 1 | // Array From () 2 | // Array.from() Method returns a new array from any array likr iterable object 3 | 4 | let names = "Suhail"; 5 | 6 | console.log(Array.from(names)); // [ 'S', 'u', 'h', 'a', 'i', 'l' ] 7 | 8 | let num = "12345"; 9 | console.log(Array.from(num, (ele) => ele * ele)); 10 | console.log(num); 11 | -------------------------------------------------------------------------------- /7. Array-Methods/16. includes().js: -------------------------------------------------------------------------------- 1 | // Array Includes 2 | 3 | const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 4 | console.log(arr1.includes(10)); 5 | 6 | // Array Includes value in true or false 7 | // The includes() method returns: 8 | 9 | // true if searchValue is found anywhere within the array 10 | // false if searchValue is not found anywhere within the array 11 | 12 | // Array Includes check the given element is 13 | // present in the array or not 14 | // 15 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 16 | 17 | console.log(arr.includes(11)); // false 18 | console.log(arr.includes(4)); // true 19 | // 11 is not in the array so it gave false 20 | // 4 is in the array so it gave true 21 | 22 | // The includes() method is case sensitive. For example: 23 | 24 | const words = ["Java", "Python", "C"]; 25 | console.log(words.includes("c")); // false 26 | // There is no lowerCase c in the array 27 | console.log(words.includes("C")); // true 28 | // There is UpperCase C in the array 29 | 30 | 31 | // 2nd Parameter is for to check from a specific index of array 32 | const test = [0, 1, 2, 3, 4, 5, 6, 7, 8]; 33 | console.log(test.includes(4, 5)); // Checking 4 from 5th index // false 34 | console.log(test.includes(4, 1)); // Checking 4 from 1st index // true 35 | 36 | -------------------------------------------------------------------------------- /7. Array-Methods/17. indexof().js: -------------------------------------------------------------------------------- 1 | // Indexof Array Method 2 | 3 | // Array Indexof gives the index of the specifc element from 4 | // the array 5 | 6 | // IF THERE IS NO INDEX OF THAT ELEMENT IF GIVE RETURN -1 7 | 8 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 9 | 10 | console.log(arr.indexOf(1)); // 0 11 | console.log(arr.indexOf(2)); // 1 12 | console.log(arr.indexOf(10)); // 9 13 | 14 | const words = ["Java", "C", "C++", "Python"]; 15 | // Indexs are 0 1 2 3 16 | 17 | console.log(words.indexOf("JavaScript")); // -1 18 | console.log(words.indexOf("C++")); //2; 19 | console.log(words.indexOf("Java")); // 0 20 | console.log(words.at(0)); 21 | -------------------------------------------------------------------------------- /7. Array-Methods/18. isArray().js: -------------------------------------------------------------------------------- 1 | // Array is.Array() // 2 | // Checks the array is a array or not 3 | 4 | // Returns true if the array is true 5 | // Returns false if the array is false 6 | 7 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 8 | 9 | console.log(arr); 10 | 11 | console.log(Array.isArray(arr)); 12 | 13 | // 14 | 15 | const arr2 = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 16 | console.log(arr2); 17 | 18 | console.log(Array.isArray(arr2)); 19 | 20 | console.log(typeof arr2); // Its an Object but shows as Array 21 | 22 | 23 | // passing an empty array [] 24 | console.log(Array.isArray([])); // true 25 | 26 | // we have created an array with element 7 and 27 | // passed that value to isArray() 28 | console.log(Array.isArray(new Array(7))); // true 29 | 30 | // passing a boolean value 31 | console.log(Array.isArray(true)); // false 32 | 33 | // passing undefined 34 | console.log(Array.isArray(undefined)); // false 35 | 36 | // not passing any argument in isArray() 37 | console.log(Array.isArray()); // false 38 | -------------------------------------------------------------------------------- /7. Array-Methods/19. join().js: -------------------------------------------------------------------------------- 1 | // Array Join() Method 2 | // The join() method returns a new string by 3 | // concatenating all of the elements in an array, 4 | // separated by a specified separator. 5 | 6 | /* 7 | The join() method does not change the original array. 8 | Elements like undefined, null, or empty array have an 9 | empty string representation. 10 | */ 11 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 12 | 13 | console.log(arr.join()); 14 | // 1,2,3,4,5,6,7,8,9,10 15 | 16 | console.log(arr.join("")); 17 | // 12345678910 18 | 19 | console.log(arr.join(" | ")); 20 | // 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; 21 | 22 | const arr2 = ["Java", "C", "C++"]; 23 | console.log(arr2.join(" ")); 24 | // Java C C++ 25 | 26 | console.log(arr.join("+")); 27 | // 1+2+3+4+5+6+7+8+9+10 28 | -------------------------------------------------------------------------------- /7. Array-Methods/2. concat().js: -------------------------------------------------------------------------------- 1 | // Array Concat () 2 | 3 | // It will concat multiple arrays and returns a new array 4 | 5 | const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 6 | const arr2 = ["Java", "C", "C++", "Python", "Javascript"]; 7 | 8 | console.log(arr1.concat(arr2)); 9 | 10 | // [ 11 | // 1, 2, 12 | // 3, 4, 13 | // 5, 6, 14 | // 7, 8, 15 | // 9, 10, 16 | // 'Java', 'C', 17 | // 'C++', 'Python', 18 | // 'Javascript' 19 | // ] 20 | 21 | const arr3 = [2, 4, 6, 8, 10]; 22 | const arr4 = [1, 3, 5, 7, 9]; 23 | 24 | // Concatenating Multiple Arrays 25 | console.log(arr1.concat(arr2, arr3, arr4)); 26 | /* 27 | [ 28 | 1, 2, 3, 29 | 4, 5, 6, 30 | 7, 8, 9, 31 | 10, 'Java', 'C', 32 | 'C++', 'Python', 'Javascript', 33 | 2, 4, 6, 34 | 8, 10, 1, 35 | 3, 5, 7, 36 | 9 37 | ] 38 | 39 | */ 40 | 41 | // Adding Multiple Values While Concatenating 42 | console.log(arr1.concat("Hello", arr2, arr3, arr4)); 43 | /* 44 | [ 45 | 1, 2, 3, 46 | 4, 5, 6, 47 | 7, 8, 9, 48 | 10, 'Hello', 'Java', 49 | 'C', 'C++', 'Python', 50 | 'Javascript', 2, 4, 51 | 6, 8, 10, 52 | 1, 3, 5, 53 | 7, 9 54 | ] 55 | */ 56 | 57 | -------------------------------------------------------------------------------- /7. Array-Methods/20. keys().js: -------------------------------------------------------------------------------- 1 | // Array Keys() 2 | // The keys() method returns a new Array Iterator object that contains 3 | // the keys for each element in the array. 4 | 5 | const arr = [1,2,3,4,5,6,7,8,9,10] 6 | 7 | let store = arr.keys(); 8 | console.log(store); 9 | 10 | for (let i of store) { 11 | console.log(i); 12 | } 13 | 14 | 15 | console.log(store.next().value); 16 | console.log(store.next().value); 17 | console.log(store.next().value); 18 | console.log(store.next().value); 19 | console.log(store.next().value); 20 | console.log(store.next().value); 21 | const words = ["Hello", "I am", "Suhail"]; 22 | console.log(words); 23 | 24 | for (let i of words.keys()) { 25 | console.log(i); 26 | } 27 | -------------------------------------------------------------------------------- /7. Array-Methods/21. lastindexof().js: -------------------------------------------------------------------------------- 1 | // Array lastIndexof() 2 | //The lastIndexOf() method returns the index of 3 | // the last occurrence of a specified element in the array. 4 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]; 5 | 6 | console.log(arr.lastIndexOf(1)); 7 | console.log(arr.at(9)); 8 | console.log(arr.indexOf(1)); 9 | 10 | -------------------------------------------------------------------------------- /7. Array-Methods/23. of().js: -------------------------------------------------------------------------------- 1 | // Array of() 2 | // The of() method creates a new Array instance from the given arguments. 3 | 4 | console.log(Array.of(12, 4, 4, 5, 34, 3, 4, 5, 5, 4)); 5 | 6 | let data = Array.of("Hello", "Java", "C", "C++"); 7 | console.log(data); 8 | console.log(typeof data); 9 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 10 | console.log(typeof arr); 11 | const aarr2 = new Array(1, 2, 4, 5, 65, 6, 4, 4, 43, 3, 3, 3, 3); 12 | console.log(aarr2); 13 | -------------------------------------------------------------------------------- /7. Array-Methods/24. pop().js: -------------------------------------------------------------------------------- 1 | // Array pop() 2 | 3 | // The pop() method removes the last element from an array and returns that element. 4 | 5 | let cities = ["Madrid", "New York", "Kathmandu", "Paris"]; 6 | 7 | // remove the last element 8 | let removedCity = cities.pop(); 9 | 10 | console.log(cities); // ["Madrid", "New York", "Kathmandu"] 11 | console.log(removedCity); // Paris 12 | -------------------------------------------------------------------------------- /7. Array-Methods/25. push().js: -------------------------------------------------------------------------------- 1 | // Array Push() 2 | // The push() method adds zero or more elements to the end of the array. 3 | 4 | let city = ["New York", "Madrid", "Kathmandu"]; 5 | 6 | // add "London" to the array 7 | city.push("London"); 8 | 9 | console.log(city); 10 | 11 | // Output: [ 'New York', 'Madrid', 'Kathmandu', 'London' ] 12 | -------------------------------------------------------------------------------- /7. Array-Methods/26. reduce().js: -------------------------------------------------------------------------------- 1 | // Array Reduce() 2 | // The reduce() method executes a reducer function 3 | // on each element of the array and returns a single output value. 4 | 5 | const values = [3, 5, 1, 4, 2]; 6 | 7 | const total = values.reduce((acc, item) => { 8 | return acc + item; 9 | }, 0); 10 | // console.log(total); // 15 11 | // acc = 0 and item = 3 ====> 0+3 = 3 12 | // acc = 3 and item = 5 ====> 3+5 = 8 13 | // acc = 8 and item = 1 ====> 8+1 = 9 14 | // acc = 9 and item = 4 ====> 9+4 = 13 15 | // acc = 13 and item = 2 ===> 13+2 = 15 16 | 17 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 18 | 19 | // console.log( 20 | // arr.reduce((acc, count) => { 21 | // return acc + count; 22 | // }, 0) 23 | // ); 24 | 25 | function sum_reducer(accumulator, currentValue) { 26 | return accumulator + currentValue; 27 | } 28 | 29 | // let sum = arr.reduce(sum_reducer); 30 | // console.log(sum); // 21 31 | 32 | const arr1 = [13, 2, 5]; 33 | let data = arr1.reduce((init, value, index, array) => { 34 | // Start value starts from 0 35 | console.log(init); 36 | // 1st value of arr1 13 37 | console.log(value); 38 | // index of 1st value 0 39 | console.log(index); 40 | // console whole array [13,2,5] 41 | console.log(array); 42 | 43 | return init + value; 44 | }, 0); 45 | console.log(data); 46 | -------------------------------------------------------------------------------- /7. Array-Methods/27. reduceRight().js: -------------------------------------------------------------------------------- 1 | // Javascript Array reduceRight() 2 | 3 | // The reduceRight() method reduces the array to a 4 | // single value by executing a callback function on 5 | // two values of the array (from right to left). 6 | const arr1 = [13, 2, 5]; 7 | let data = arr1.reduceRight((init, value, index, array) => { 8 | // Start value starts from 0 9 | console.log(init); 10 | // last value of arr1 5 11 | console.log(value); 12 | // index of last value 0 13 | console.log(index); 14 | // console whole array [13,2,5] 15 | console.log(array); 16 | 17 | return init + value; 18 | }, 0); 19 | console.log(data); 20 | 21 | // acc = 0 and item = 5 ====> 0+5 = 5 22 | // acc = 5 and item = 2 ====> 5+2 = 7 23 | // acc = 7 and item = 13 ====>7+13 = 20 24 | -------------------------------------------------------------------------------- /7. Array-Methods/28. reverse().js: -------------------------------------------------------------------------------- 1 | // JavaScript Array reverse() 2 | 3 | // The reverse() method returns the array in reverse order. 4 | 5 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 6 | let data = arr.reverse(); 7 | 8 | console.log(data); 9 | console.log(arr); 10 | -------------------------------------------------------------------------------- /7. Array-Methods/29. shift().js: -------------------------------------------------------------------------------- 1 | // JavaScript Array shift() 2 | 3 | // The shift() method removes the first 4 | // element from an array and returns that element. 5 | 6 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 7 | arr.shift(); 8 | 9 | console.log(arr); 10 | /* 11 | [ 12 | 2, 3, 4, 5, 6, 13 | 7, 8, 9, 10 14 | ] 15 | */ 16 | -------------------------------------------------------------------------------- /7. Array-Methods/3. constructor().js: -------------------------------------------------------------------------------- 1 | // Array Constructor() 2 | 3 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 4 | 5 | let store = arr.constructor; 6 | 7 | console.log(store); 8 | 9 | // [Function: Array] -------------------------------------------------------------------------------- /7. Array-Methods/30. slice().js: -------------------------------------------------------------------------------- 1 | // JavaScript Array slice() 2 | 3 | // The slice() method returns a shallow 4 | // copy of a portion of an array into a new array object. 5 | 6 | const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 7 | // 1 2 3 4 5 6 7 8 9 10 11 8 | 9 | console.log(arr.slice()); // Gives a whole array 10 | // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 11 | 12 | console.log(arr.slice(4, 8)); 13 | // Slice is == start,end+1 14 | 15 | // 1st parameter Start from 0 16 | // 2nd parameter Start from 1 17 | // 0 1 2 3 4 18 | let languages = ["JavaScript", "Python", "C", "C++", "Java"]; 19 | // 1 2 3 4 5 20 | console.log(languages.slice(0, 2)); 21 | 22 | 23 | 24 | // Negative value 25 | 26 | console.log(languages.slice(0,-1)); // removes last array 27 | // [ 'JavaScript', 'Python', 'C', 'C++' ] 28 | console.log(languages.slice(0,-2)); // removes last 2 array's 29 | // [ 'JavaScript', 'Python', 'C' ] 30 | 31 | 32 | 33 | 34 | 35 | console.log(languages.slice(-2)); // Gets last 2 arrays 36 | // [ 'C++', 'Java' ] -------------------------------------------------------------------------------- /7. Array-Methods/31. some().js: -------------------------------------------------------------------------------- 1 | // Javascript Array some() 2 | 3 | // The some() method tests whether any of the array 4 | // elements pass the given test function. 5 | 6 | // array of scores obtained by student 7 | 8 | // Checks atleast one of the conition passes or not 9 | 10 | // some will return true if any conition is true 11 | 12 | let scoreObtained = [115, 110, 139, 78, 5, 120]; 13 | 14 | let data = scoreObtained.some((x) => (x < 40 ? true : false)); 15 | 16 | console.log(data); 17 | 18 | // Using Functions 19 | 20 | function fail(num) { 21 | return num < 40; 22 | } 23 | 24 | let data1 = scoreObtained.some(fail); 25 | console.log(data1); // true 26 | -------------------------------------------------------------------------------- /7. Array-Methods/32. sort().js: -------------------------------------------------------------------------------- 1 | // JavaScript Array sort() 2 | 3 | // The sort() method sorts the items of an 4 | // array in a specific order (ascending or descending). 5 | // All undefined elements are sorted to the end of the array. 6 | let city = ["California", "Barcelona", "Paris", "Kathmandu"]; 7 | 8 | // sort the city array in ascending order 9 | let sortedArray = city.sort(); 10 | console.log(sortedArray); 11 | console.log(city); 12 | 13 | // Output: [ 'Barcelona', 'California', 'Kathmandu', 'Paris' ] 14 | 15 | const arr = [10, 21, 13, 14, 51, 61, 71, 18, 19, 100]; 16 | console.log(arr.sort()); 17 | // [1, 10, 2, 3, 4,5, 6, 7, 8, 9] 18 | // It added all 1 numbers first 19 | 20 | // To arrange in Ascending to Descending order 21 | 22 | console.log(arr.sort((a, b) => a - b)); 23 | // It changes the Original Array 24 | 25 | // To Arrange in Desceding to Ascending Order 26 | 27 | console.log(arr.sort((a, b) => b - a)); 28 | -------------------------------------------------------------------------------- /7. Array-Methods/33. splice().js: -------------------------------------------------------------------------------- 1 | // JavaScript Array splice() 2 | 3 | // The splice() method returns an array by 4 | // changing (adding/removing) its elements in place. 5 | 6 | // 0 1 2 3 4 5 7 | let prime_numbers = [2, 3, 5, 7, 9, 11]; 8 | // 9 | 10 | // 1st Parameter = what to remove index 11 | // 2nd parameter = delete count starts from 1 12 | // 3rd parameter = elements you want to add 13 | console.log(prime_numbers.splice(4, 1, 108)); 14 | // in the index of 4 ...add this 1 element which is 108 15 | // [ 2, 3, 5, 7, 108, 11 ] 16 | console.log(prime_numbers); 17 | 18 | console.log(languages); // [ 'JavaScript', 'Java', 'Lua', 'Python', 'C', 'C++' ] 19 | let removed2 = languages.splice(2, 3); 20 | // From Index 2 delete 1 2 3 elements from there 21 | console.log(languages); // [ 'JavaScript', 'Java', 'C++ 22 | 23 | let languages = ["JavaScript", "Python", "Java", "Lua"]; 24 | 25 | // removes everything from start 26 | let removed = languages.splice(1); 27 | console.log(removed); // [ "Python", "Java", "Lua" ] 28 | console.log(languages); // [ "JavaScript" ] -------------------------------------------------------------------------------- /7. Array-Methods/35. toString().js: -------------------------------------------------------------------------------- 1 | // Javascript Array toString() 2 | 3 | // The toString() method returns a string formed by the elements of the given array. 4 | 5 | // defining an array 6 | let items = ["JavaScript", 1, "a", 3]; 7 | 8 | // returns a string with elements of the array separated by commas 9 | let itemsString = items.toString(); 10 | 11 | console.log(itemsString); 12 | 13 | // Output: 14 | // JavaScript,1,a,3 15 | 16 | /* 17 | The toString() method does not change the original array. 18 | Elements like undefined, null, or empty array, have an empty 19 | string representation. 20 | 21 | */ // defining a nested array 22 | let nestedArray = [1, 2, 4, ["Apple", 5]]; 23 | 24 | // returns string representation of the nested array by flattening the array 25 | let resultingArray = nestedArray.toString(); 26 | 27 | console.log(resultingArray); 28 | -------------------------------------------------------------------------------- /7. Array-Methods/36. unshift().js: -------------------------------------------------------------------------------- 1 | // Array Unshift() 2 | // The unshift() method adds one or more 3 | // elements to the beginning of an array and 4 | // returns the new length of the array. 5 | 6 | let languages = ["Java", "Python", "C"]; 7 | 8 | // add "JavaScript" at the beginning of the array 9 | languages.unshift("JavaScript"); 10 | console.log(languages); 11 | 12 | // Output: [ 'JavaScript', 'Java', 'Python', 'C' ] 13 | 14 | var languages1 = ["JavaScript", "Python", "Java", "Lua"]; 15 | 16 | var count = languages1.unshift("C++"); 17 | console.log(languages1); // [ 'C++', 'JavaScript', 'Python', 'Java', 'Lua' ] 18 | console.log(count); // 5 19 | 20 | var priceList = [12, 21, 35]; 21 | 22 | var count1 = priceList.unshift(44, 10, 1.6); 23 | console.log(priceList); // [ 44, 10, 1.6, 12, 21, 35 ] 24 | console.log(count1); // 6 25 | -------------------------------------------------------------------------------- /7. Array-Methods/37. values().js: -------------------------------------------------------------------------------- 1 | // JavaScript Array values() 2 | 3 | // The values() method returns a new Array 4 | // Iterator object that contains the values for 5 | // each index in the array. 6 | 7 | let languages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 8 | 9 | // returns an Array Iterator object that contain values 10 | let iteratorObject = languages.values(); 11 | 12 | // looping through iterator 13 | for (let value of iteratorObject) { 14 | console.log(value); 15 | } 16 | 17 | // Output: 18 | // JavaScript 19 | // Java 20 | // C++ 21 | 22 | // Notes: The value() method does not change the original array. 23 | 24 | 25 | // console.log(iteratorObject.next().value); 26 | 27 | let arrayWithHole = ["A", "B", , "C"]; 28 | 29 | // returns 'undefined' as a value for empty slot 30 | let iteratorObject1 = arrayWithHole.values(); 31 | 32 | // looping through iterator 33 | // for (let value of iteratorObject1) { 34 | // console.log(value); 35 | // } 36 | 37 | // Output; 38 | 39 | // A; 40 | // B; 41 | // undefined; 42 | // C; 43 | 44 | // Example 3: More About Array Iterator Object 45 | // The array iterator object does not store values rather it stores 46 | // the address of the array used. For example: 47 | 48 | let fruits = ["Apple", "Banana", "Grapes"]; 49 | 50 | // returns values of each index in the fruits array 51 | let iteratorObject2 = fruits.values(); 52 | 53 | // accessing the value of index 0 in the array 54 | // console.log(iteratorObject2.next().value); // Apple 55 | 56 | // changing the value of index 1 57 | fruits[1] = "Cherry"; 58 | 59 | // accessing the value of index 1 in the array 60 | // console.log(iteratorObject2.next().value); // Cherry 61 | // Output 62 | 63 | // Apple 64 | // Cherry -------------------------------------------------------------------------------- /7. Array-Methods/38. length().js: -------------------------------------------------------------------------------- 1 | // JavaScript Array length() 2 | 3 | // The length property returns or sets 4 | // the number of elements in an array. 5 | 6 | // It starts the length from 1 7 | 8 | let city = ["California", "Barcelona", "Paris", "Kathmandu"]; 9 | 10 | // find the length of the city array 11 | let len = city.length; 12 | console.log(len); 13 | 14 | // Output: 4 15 | var companyList = ["Apple", "Google", "Facebook", "Amazon"]; 16 | console.log(companyList.length); // Output: 4 17 | 18 | var randomList = ["JavaScript", 44]; 19 | console.log(randomList.length); // Output: 2 20 | 21 | var emptyArray = []; 22 | console.log(emptyArray.length); // Output: 0 23 | 24 | var languages = ["JavaScript", "Python", "C++", "Java", "Lua"]; 25 | 26 | // languages.length can be used to find out 27 | // the number of times to loop over an array 28 | for (i = 0; i < languages.length; i++) { 29 | console.log(languages[i]); 30 | } 31 | 32 | var languages = ["JavaScript", "Python", "C++", "Java", "Lua"]; 33 | 34 | // truncate the Array to 3 elements 35 | languages.length = 3; 36 | 37 | // Output: [ 'JavaScript', 'Python', 'C++' ] 38 | console.log(languages); 39 | 40 | // extend the Array to length 6 41 | languages.length = 6; 42 | 43 | // Output: [ 'JavaScript', 'Python', 'C++', <3 empty items> ] 44 | console.log(languages); 45 | 46 | // [ 'JavaScript', 'Python', 'C++' ] 47 | // [ 'JavaScript', 'Python', 'C++', <3 empty item> ] 48 | 49 | 50 | // Here, we first truncated the length of Array as 3 (assigned value) 51 | // is less than 4 (original Array length). 52 | 53 | // If the assigned value is more than the original Array length, empty 54 | // items are appended to the end of the Array. We can see this happen 55 | // in the second example. -------------------------------------------------------------------------------- /7. Array-Methods/4. copyWithin().js: -------------------------------------------------------------------------------- 1 | // Array copyWithIn() 2 | // Copy the elememnt from 1 index to another index 3 | 4 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 5 | /// Index 0 1 2 6 | // It Takes 2 Parameters 7 | // 1st - Index of the element to be changed 8 | // 2nd - Index of the element to be taken from 9 | // 3rd - Till Where the Index of Excluding] 10 | 11 | console.log(arr.copyWithin(0, 2, 3)); 12 | // 0 index element is 1 13 | // 1 is going to change to index value of 2 14 | // index value of 2 is 3 15 | // so 1 ==> 3 16 | 17 | // Output 18 | // [3, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] 19 | 20 | let words = ["apple", "ball", "cat", "dog"]; 21 | 22 | console.log(words.copyWithin(1, 2, 3)); 23 | 24 | console.log(words); 25 | 26 | // It changes the original Array 27 | 28 | // [ 'apple', 'cat', 'cat', 'dog' ] 29 | -------------------------------------------------------------------------------- /7. Array-Methods/5. entries().js: -------------------------------------------------------------------------------- 1 | // Array Entries() 2 | // It returns a array with object key/value pairs in the 3 | // form of indexs 4 | // The entries() method returns a new Array 5 | // Iterator object containing key/value pairs 6 | // for each array index. 7 | 8 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 9 | 10 | let store = arr.entries(); 11 | 12 | // 13 | // console.log(store.next().value); 14 | // console.log(store.next().value); 15 | // console.log(store.next().value); 16 | // console.log(store.next().value); 17 | // console.log(store.next().value); 18 | // console.log(store.next().value); 19 | 20 | 21 | for (let i of store) { 22 | console.log(i); 23 | } 24 | 25 | // console.log(arr); 26 | 27 | 28 | /* 29 | [ 0, 1 ] 30 | [ 1, 2 ] 31 | [ 2, 3 ] 32 | [ 3, 4 ] 33 | [ 4, 5 ] 34 | [ 5, 6 ] 35 | [ 6, 7 ] 36 | [ 7, 8 ] 37 | [ 8, 9 ] 38 | [ 9, 10 ] 39 | */ -------------------------------------------------------------------------------- /7. Array-Methods/6. every().js: -------------------------------------------------------------------------------- 1 | // Array Every() 2 | // So Array Every Returns a Boolean Value 3 | // Checks whether the condition given in the parameter 4 | // is true or false 5 | 6 | // Checks all should match the condition 7 | // if all match then true 8 | // else false 9 | 10 | // every will return true if all every predicate is true 11 | const arr = [2, 4, 6, 8, 10]; 12 | 13 | // console.log(arr.every((x) => x % 2 == 0)); // true 14 | // OR 15 | 16 | // function isEven(num) { 17 | // return !(num & 1); 18 | // } 19 | // console.log(arr.every(search)); 20 | 21 | // 22 | 23 | function isEven(num) { 24 | return !(num & 1); 25 | } 26 | 27 | console.log("This is every"); 28 | 29 | console.log(arr.every(isEven)); 30 | -------------------------------------------------------------------------------- /7. Array-Methods/7. fill().js: -------------------------------------------------------------------------------- 1 | // Array fill() Method 2 | // It fills the all the elements with the given parameter 3 | // It also takes 3 parameters 4 | // Target , start , end 5 | // It Changes the Original Array 6 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 7 | console.log(arr); 8 | 9 | console.log(arr.fill(20, 4, 9)); 10 | console.log(arr); 11 | 12 | const arr1 = ["Java", "C++", "Javascript"]; 13 | 14 | console.log(arr1.fill("Hello")); 15 | console.log(arr1); 16 | -------------------------------------------------------------------------------- /7. Array-Methods/8. filter().js: -------------------------------------------------------------------------------- 1 | // The filter() method returns a new array 2 | // with all elements that pass the test 3 | // defined by the given function. 4 | 5 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 6 | 7 | // console.log(arr.filter((x) => x >= 5)); 8 | 9 | let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 10 | 11 | // function to check even numbers 12 | function checkEven(number) { 13 | if (number % 2 == 0) return true; 14 | else return false; 15 | } 16 | 17 | // create a new array by filter even numbers from the numbers array 18 | let evenNumbers = numbers.filter(checkEven); 19 | // console.log(evenNumbers); 20 | 21 | // Output: [ 2, 4, 6, 8, 10 ] 22 | 23 | const languages = [ 24 | "JavaScript", 25 | "Python", 26 | "Ruby", 27 | "C", 28 | "C++", 29 | "Swift", 30 | "PHP", 31 | "Java", 32 | ]; 33 | 34 | // function search(arr, letter) { 35 | // for (let i of arr) { 36 | // if (i.includes(letter)) { 37 | // console.log(i); 38 | // } 39 | // } 40 | // } 41 | 42 | // search(languages, "Sw"); 43 | 44 | const search = (arr, letter) => 45 | arr.filter((x) => { 46 | if (x == letter) { 47 | console.log(x); 48 | } 49 | }); 50 | search(languages, "Swift"); 51 | -------------------------------------------------------------------------------- /7. Array-Methods/9. find().js: -------------------------------------------------------------------------------- 1 | // Array Find() 2 | // Array Find Method Returns the First Element from ther 3 | // Array Which passes the test given in the defined in 4 | // the function 5 | 6 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 7 | 8 | console.log(arr.find((x) => x % 2 == 0)); // 2 9 | 10 | // 2 is the first element from finding all even numbers 11 | 12 | 13 | -------------------------------------------------------------------------------- /7. Array-Methods/all-methods.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/7. Array-Methods/all-methods.jpg -------------------------------------------------------------------------------- /8. String-Methods/1. charAt.js: -------------------------------------------------------------------------------- 1 | let names = "Suhail"; 2 | 3 | let result = names.charAt(0) 4 | 5 | console.log(result); // S 6 | 7 | // Or Else you can do 8 | 9 | console.log(names[0]); 10 | 11 | 12 | -------------------------------------------------------------------------------- /8. String-Methods/11. localeCompare().js: -------------------------------------------------------------------------------- 1 | let msg = "f"; 2 | let a = msg.localeCompare("f"); 3 | console.log(a); // 0 4 | 5 | let msg1 = "b"; 6 | let a1 = msg1.localeCompare("a"); 7 | console.log(a1); // 1 8 | 9 | let msg2 = "a"; 10 | let a2 = msg2.localeCompare("b"); 11 | console.log(a2); // -1 12 | -------------------------------------------------------------------------------- /8. String-Methods/12. match().js: -------------------------------------------------------------------------------- 1 | let a = "Hello Iam Suhail"; 2 | let reg = /Hello/; 3 | let result = a.match(reg); 4 | console.log(result); 5 | -------------------------------------------------------------------------------- /8. String-Methods/13. matchAll().js: -------------------------------------------------------------------------------- 1 | let n1 = "Hi I am Suhail...Full Name Suhail Roushan"; 2 | 3 | let out = n1.matchAll("Suhail"); 4 | 5 | for (i of out) { 6 | console.log(i); 7 | } 8 | -------------------------------------------------------------------------------- /8. String-Methods/14. padEnd().js: -------------------------------------------------------------------------------- 1 | // 14. padEnd() 2 | 3 | let n9 = "CODE"; 4 | console.log(n9.padEnd(10, "*")); 5 | -------------------------------------------------------------------------------- /8. String-Methods/15. padStart().js: -------------------------------------------------------------------------------- 1 | // 15. padStart() 2 | 3 | let n10 = "Suhail"; 4 | console.log(n10.padStart(10, "*")); 5 | -------------------------------------------------------------------------------- /8. String-Methods/16. repeat().js: -------------------------------------------------------------------------------- 1 | // 16. repeat() 2 | 3 | let n11 = "Hi"; 4 | console.log(n11.repeat(10)); 5 | -------------------------------------------------------------------------------- /8. String-Methods/17. replace().js: -------------------------------------------------------------------------------- 1 | // 17. replace 2 | 3 | let n12 = "Hi I am Suhail Roushan"; 4 | console.log(n12.replace("Suhail", "Rayhaan")); 5 | -------------------------------------------------------------------------------- /8. String-Methods/18. replaceAll().js: -------------------------------------------------------------------------------- 1 | // 18. replaceAll 2 | 3 | let n13 = "Hi I am Suhail Roushan"; 4 | 5 | console.log(n3.replaceAll("a", "i")); 6 | -------------------------------------------------------------------------------- /8. String-Methods/19. search().js: -------------------------------------------------------------------------------- 1 | // 19. Search() 2 | let n14 = "Hi I am Suhail Roushan"; 3 | 4 | console.log(n14.search("Suhail")); // 0 5 | console.log(n14.search("Hii")); // -1 6 | -------------------------------------------------------------------------------- /8. String-Methods/2. charCodeAt().js: -------------------------------------------------------------------------------- 1 | let names = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 2 | 3 | let result = names.charCodeAt(0); 4 | 5 | console.log(result); 6 | 7 | for (let i = 0; i <= names.length - 1; i++) { 8 | let out = names.charCodeAt(i); 9 | console.log(out); 10 | } 11 | -------------------------------------------------------------------------------- /8. String-Methods/20. slice().js: -------------------------------------------------------------------------------- 1 | // 20. slice() 2 | // 0123456 3 | let n15 = "ABCDEFG"; 4 | // 1234567 5 | 6 | console.log(n15.slice(3, 6)); 7 | -------------------------------------------------------------------------------- /8. String-Methods/21. split().js: -------------------------------------------------------------------------------- 1 | // 21.split 2 | 3 | let n16 = "A.B.C.D.E.F"; 4 | 5 | console.log(n16.split(".")); 6 | -------------------------------------------------------------------------------- /8. String-Methods/22. startsWith().js: -------------------------------------------------------------------------------- 1 | // 22.startsWith() 2 | 3 | let n17 = "Hi I am Suhail"; 4 | console.log(n17.startsWith("I am")); // false 5 | console.log(n17.startsWith("Hi")); // true 6 | -------------------------------------------------------------------------------- /8. String-Methods/23. substring().js: -------------------------------------------------------------------------------- 1 | // 23. substring 2 | // 012345 3 | let n18 = "Suhail"; 4 | // 123456 5 | console.log(n18.substring(3, 6)); 6 | -------------------------------------------------------------------------------- /8. String-Methods/24. toLowerCase().js: -------------------------------------------------------------------------------- 1 | // 24. toLowerCase() 2 | 3 | let n19 = "SUHAIL"; 4 | console.log(n19.toLowerCase()); 5 | -------------------------------------------------------------------------------- /8. String-Methods/25. toUpperCase().js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/8. String-Methods/25. toUpperCase().js -------------------------------------------------------------------------------- /8. String-Methods/26. trim().js: -------------------------------------------------------------------------------- 1 | // 26.trim() 2 | 3 | let n21 = " A B C "; 4 | console.log(n21.trim()); 5 | -------------------------------------------------------------------------------- /8. String-Methods/3. codePointAt().js: -------------------------------------------------------------------------------- 1 | let names = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 2 | 3 | let result = names.codePointAt(0); 4 | 5 | console.log(result); 6 | 7 | for (let i = 0; i <= names.length - 1; i++) { 8 | let out = names.codePointAt(i); 9 | console.log(out); 10 | } 11 | -------------------------------------------------------------------------------- /8. String-Methods/4. concat().js: -------------------------------------------------------------------------------- 1 | let a1 = "Suhail"; 2 | let a2 = "Roushan"; 3 | 4 | console.log(a1.concat(" ", a2)); // Suhail Roushan 5 | console.log("".concat({})); // [object Object] 6 | console.log("".concat(null)); // null 7 | console.log("".concat(true)); // true 8 | console.log("".concat(4, 5)); // 45 9 | 10 | let str1 = "Hello"; 11 | let str2 = "World"; 12 | 13 | // concatenating two strings 14 | let newStr = str1.concat(", ", str2, "!"); 15 | console.log(newStr); // Hello, World! 16 | -------------------------------------------------------------------------------- /8. String-Methods/8. includes().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript String includes() 3 | In this tutorial, we will learn about the JavaScript String includes() method with the help of examples. 4 | 5 | The includes() method checks if one string can be found inside another string. 6 | 7 | Example 8 | const message = "JavaScript is fun"; 9 | 10 | // check if message includes the string "Java" 11 | let result = message.includes("Java"); 12 | console.log(result); 13 | 14 | // Output: true 15 | Run Code 16 | includes() Syntax 17 | The syntax of the includes() method is: 18 | 19 | str.includes(searchString, position) 20 | Here, str is a string. 21 | 22 | includes() Parameters 23 | The includes() method takes in: 24 | 25 | searchString - A string to be searched for within str. 26 | position (optional) - The position within str to begin searching for searchString. By default, it is 0. 27 | includes() Return Value 28 | Returns true if searchString is found anywhere within str. 29 | Returns false if searchString is not found anywhere within str. 30 | Note: The includes() method is case sensitive. 31 | 32 | Example: Using includes() method 33 | let sentence = "Java is to JavaScript what Car is to Carpet."; 34 | 35 | let check = sentence.includes("Java"); 36 | console.log(check); // true 37 | 38 | // case sensitive 39 | let check1 = sentence.includes("java"); 40 | console.log(check1); // false 41 | 42 | // second argument specifies position to start at 43 | let check2 = sentence.includes("Java", 20); 44 | console.log(check2); // false 45 | 46 | let check3 = sentence.includes("whose"); 47 | console.log(check3); // false 48 | 49 | let check4 = sentence.includes(""); 50 | console.log(check4); // true 51 | Run Code 52 | Output 53 | 54 | true 55 | false 56 | false 57 | false 58 | true 59 | 60 | 61 | 62 | 63 | 64 | 65 | */ 66 | -------------------------------------------------------------------------------- /8. String-Methods/list.js: -------------------------------------------------------------------------------- 1 | // Total 26 String Methods In JavaScript 2 | 3 | /* 4 | 1.charAt() 5 | 2.charCodeAt() 6 | 3.codePointAt() 7 | 4.concat() 8 | 5.endsWith() 9 | 6.fromCharCode() 10 | 7.fromCodePoint() 11 | 8.includes() 12 | 9.indexOFf() 13 | 10.length() 14 | 11.localeCompare() 15 | 12.match() 16 | 13.matchAll() 17 | 14.padEnd() 18 | 15.padStart() 19 | 16.repeat() 20 | 17.replace() 21 | 18.replaceAll() 22 | 19.slice() 23 | 20.split() 24 | 22.substring() 25 | 23.startsWith() 26 | 24.toLowerCase() 27 | 25.toUpperCase() 28 | 26.trim() 29 | 30 | */ -------------------------------------------------------------------------------- /9. Object-Methods/1. Object.assign.js: -------------------------------------------------------------------------------- 1 | let a1 = { 2 | name: "Suhail", 3 | age: 20, 4 | }; 5 | 6 | let newA1 = {}; 7 | 8 | let copy = Object.assign(newA1, a1); 9 | console.log(newA1);//{ name: 'Suhail', age: 20 } 10 | newA1.ok = "Hi" 11 | console.log(newA1); //{ name: 'Suhail', age: 20, ok: 'Hi' } 12 | 13 | console.log(copy == newA1); // true 14 | console.log(copy); //{ name: 'Suhail', age: 20, ok: 'Hi' } 15 | 16 | // It copies whole object into an new Object with a referncene one -------------------------------------------------------------------------------- /9. Object-Methods/10. Object getOwnPropertyNames().js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Object getOwnPropertyNames() 3 | The JavaScript Object.getOwnPropertyNames() method returns an array of all properties found in a given object. 4 | 5 | The syntax of the getOwnPropertyNames() method is: 6 | 7 | Object.getOwnPropertyNames(obj) 8 | The getOwnPropertyNames() method, being a static method, is called using the Object class name. 9 | 10 | getOwnPropertyNames() Parameters 11 | The getOwnPropertyNames() method takes in: 12 | 13 | obj - The object whose enumerable and non-enumerable properties are to be returned. 14 | Return value from getOwnPropertyNames() 15 | Returns an array of strings that corresponds to the properties found directly in the given object. 16 | Note: Object.getOwnPropertyNames() returns all own properties of the object while Object.keys() returns all enumerable own properties. 17 | 18 | Example: Using getOwnPropertyNames() 19 | // array object 20 | let arr = ["a", "b", "c"]; 21 | console.log(Object.getOwnPropertyNames(arr)); // [ '0', '1', '2', 'length' ] 22 | 23 | // array-like objects 24 | let obj = { 65: "A", 66: "B", 67: "C" }; 25 | console.log(Object.getOwnPropertyNames(obj)); // [ '65', '66', '67' ] 26 | 27 | // non-enumerable properties are also returned 28 | let obj1 = Object.create( 29 | {}, 30 | { 31 | getValue: { 32 | value: function () { 33 | return this.value; 34 | }, 35 | enumerable: false, 36 | }, 37 | } 38 | ); 39 | obj1.value = 45; 40 | 41 | console.log(Object.getOwnPropertyNames(obj1)); // [ 'getValue', 'value' ] 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | */ -------------------------------------------------------------------------------- /9. Object-Methods/11. Object getOwnPropertySymbols().js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Object getOwnPropertySymbols() 3 | The JavaScript Object.getOwnPropertySymbols() method returns an array of all symbol properties found in a given object. 4 | 5 | The syntax of the getOwnPropertySymbols() method is: 6 | 7 | Object.getOwnPropertySymbols(obj) 8 | The getOwnPropertySymbols() method, being a static method, is called using the Object class name. 9 | 10 | getOwnPropertySymbols() Parameters 11 | The getOwnPropertySymbols() method takes in: 12 | 13 | obj - The object whose symbol properties are to be returned. 14 | Return value from getOwnPropertySymbols() 15 | Returns an array of all symbol properties found directly upon the given object. 16 | Note: Object.getOwnPropertySymbols() returns all symbol properties of the object while Object.getOwnPropertyNames() returns the string properties 17 | 18 | Example: Using getOwnPropertySymbols() 19 | let obj = {}; 20 | let a = Symbol("a"); 21 | let b = Symbol.for("b"); 22 | 23 | obj[a] = "localSymbolValue"; 24 | obj[b] = "globalSymbolValue"; 25 | 26 | // returns an array of symbol 27 | let objectSymbols = Object.getOwnPropertySymbols(obj); 28 | 29 | console.log(objectSymbols.length); // 2 30 | console.log(objectSymbols); // [Symbol(a), Symbol(b)] 31 | console.log(objectSymbols[0]); // Symbol(a) 32 | 33 | 34 | 35 | 36 | 37 | 38 | */ -------------------------------------------------------------------------------- /9. Object-Methods/12. Object.getPrototypeOf().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.getPrototypeOf() 3 | The JavaScript Object.getPrototypeOf() method returns the prototype of the specified object. 4 | 5 | The syntax of the getPrototypeOf() method is: 6 | 7 | Object.getPrototypeOf(obj) 8 | The getPrototypeOf() method, being a static method, is called using the Object class name. 9 | 10 | getPrototypeOf() Parameters 11 | The getPrototypeOf() method takes in: 12 | 13 | obj - The object whose prototype is to be returned. 14 | Return value from getPrototypeOf() 15 | Returns prototype of the given object. 16 | Returns null If there are no inherited properties. 17 | Example: Using Object.getPrototypeOf() 18 | let proto = {}; 19 | let obj = Object.create(proto); 20 | 21 | console.log(Object.getPrototypeOf(obj) === proto); // true 22 | 23 | console.log(Object.getPrototypeOf("JavaScript")); 24 | 25 | // function 26 | let func1 = function () {}; 27 | 28 | // creating object from function 29 | let object1 = Object.create(func1); 30 | 31 | console.log(Object.getPrototypeOf(func1) === Object.getPrototypeOf(object1)); // false 32 | 33 | // Object.create() creates object with given object as prototype 34 | console.log(func1 === Object.getPrototypeOf(object1)); // true 35 | 36 | 37 | 38 | 39 | 40 | 41 | */ -------------------------------------------------------------------------------- /9. Object-Methods/13. Object hasOwnProperty().js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Object hasOwnProperty() 3 | The JavaScript Object hasOwnProperty() method checks if the object has the given property as its own property. 4 | 5 | The syntax of the hasOwnProperty() method is: 6 | 7 | obj.hasOwnProperty(prop) 8 | Here, obj is an object. 9 | 10 | hasOwnProperty() Parameters 11 | The hasOwnProperty() method takes in: 12 | 13 | prop - The String name or Symbol of the property to test. 14 | Return value from hasOwnProperty() 15 | Returns a Boolean indicating whether or not the object has the specified property as its own property. 16 | Notes: 17 | 18 | Unlike the in operator, this method does not check for a property in the object's prototype chain. 19 | hasOwnProperty returns true even if the value of the property is null or undefined. 20 | Example: Using hasOwnProperty() 21 | const obj = {}; 22 | obj.property1 = 42; 23 | 24 | console.log(obj.hasOwnProperty("property1")); // true 25 | 26 | console.log(obj.hasOwnProperty("property2")); // false 27 | 28 | // Inherited properties return false 29 | console.log(obj.hasOwnProperty("toString")); // false 30 | 31 | 32 | 33 | 34 | 35 | 36 | */ -------------------------------------------------------------------------------- /9. Object-Methods/14. Object.is().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.is() 3 | The JavaScript Object.is() method checks if two values are the same value. 4 | 5 | The syntax of the is() method is: 6 | 7 | Object.is(value1, value2) 8 | The is() method, being a static method, is called using the Object class name. 9 | 10 | is() Parameters 11 | The is() method takes in: 12 | 13 | value1 - The first value to compare. 14 | value2 - The second value to compare. 15 | Return value from is() 16 | Returns a Boolean indicating whether or not the two arguments are the same value. 17 | Two values are the same if one of the following holds: 18 | 19 | both undefined 20 | both null 21 | both true or both false 22 | both strings of the same length with the same characters in the same order 23 | both the same object (means both object have same reference) 24 | both numbers and 25 | both +0 26 | both -0 27 | both NaN 28 | or both non-zero and both not NaN and both have the same value 29 | Example: Using Object.is() 30 | // Objects with the same values 31 | console.log(Object.is("JavaScript", "JavaScript")); // true 32 | // Objects with different values 33 | console.log(Object.is("JavaScript", "javascript")); // false 34 | 35 | console.log(Object.is([], [])); // false 36 | 37 | let obj1 = { a: 1 }; 38 | let obj2 = { a: 1 }; 39 | console.log(Object.is(obj1, obj1)); // true 40 | console.log(Object.is(obj1, obj2)); // false 41 | 42 | console.log(Object.is(null, null)); // true 43 | 44 | // Special Cases 45 | console.log(Object.is(0, -0)); // false 46 | console.log(Object.is(-0, -0)); // true 47 | console.log(Object.is(NaN, 0 / 0)); // true 48 | 49 | 50 | 51 | 52 | 53 | 54 | */ -------------------------------------------------------------------------------- /9. Object-Methods/15. Object.isExtensible().js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Object.isExtensible() 3 | The JavaScript Object.isExtensible() method checks if an object is extensible i.e. new properties can be added to it. 4 | 5 | The syntax of the isExtensible() method is: 6 | 7 | Object.isExtensible(obj) 8 | The isExtensible() method, being a static method, is called using the Object class name. 9 | 10 | isExtensible() Parameters 11 | The isExtensible() method takes in: 12 | 13 | obj - The object which should be checked. 14 | Return value from isExtensible() 15 | Returns a Boolean indicating whether or not the given object is extensible. 16 | Note: An object can be marked as non-extensible using Object.preventExtensions(), Object.seal(), or Object.freeze(). 17 | 18 | Example: Using isExtensible() 19 | // New objects are extensible. 20 | let empty = {}; 21 | console.log(Object.isExtensible(empty)); // true 22 | 23 | Object.preventExtensions(empty); 24 | console.log(Object.isExtensible(empty)); // false 25 | 26 | // Sealed objects are by definition non-extensible. 27 | let sealed = Object.seal({}); 28 | console.log(Object.isExtensible(sealed)); // false 29 | 30 | // Frozen objects are also by definition non-extensible. 31 | let frozen = Object.freeze({}); 32 | console.log(Object.isExtensible(frozen)); // false 33 | 34 | 35 | 36 | 37 | 38 | 39 | */ -------------------------------------------------------------------------------- /9. Object-Methods/16. Object.isFrozen().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.isFrozen() 3 | The JavaScript Object.isFrozen() checks if an object is frozen. 4 | 5 | A frozen object can no longer be changed. Freezing an object prevents: 6 | 7 | New properties from being added to the object. 8 | Existing properties to be removed from the object. 9 | Changing the enumerability, configurability, or writability of existing properties. 10 | Changing values of the existing object properties and prototype. 11 | The syntax of the isFrozen() method is: 12 | 13 | Object.isFrozen(obj) 14 | The isFrozen() method, being a static method, is called using the Object class name. 15 | 16 | isFrozen() Parameters 17 | The isFrozen() method takes in: 18 | 19 | obj - The object which should be checked. 20 | Return value from isFrozen() 21 | Returns a Boolean indicating whether or not the given object is frozen. 22 | Example: Using isFrozen() 23 | // new objects are extensible, so not frozen 24 | console.log(Object.isFrozen({ name: "JavaScript" })); // false 25 | 26 | // preventing extensions only does not make frozen 27 | // property is still configurable 28 | let obj = { a: 1 }; 29 | Object.preventExtensions(obj); 30 | console.log(Object.isFrozen(obj)); // false 31 | 32 | // deleting property 33 | delete obj.a; 34 | console.log(Object.isFrozen(obj)); // true -> vacuously frozen 35 | 36 | let newObj = { b: 2 }; 37 | // make non-extensible 38 | Object.preventExtensions(newObj); 39 | // make non-writable 40 | Object.defineProperty(newObj, "b", { 41 | writable: false, 42 | }); 43 | // properties are still configurable 44 | console.log(Object.isFrozen(newObj)); // false 45 | 46 | // using freeze() 47 | let frozen = { 65: "A" }; 48 | 49 | Object.freeze(frozen); 50 | console.log(Object.isFrozen(frozen)); // true 51 | 52 | 53 | 54 | 55 | 56 | 57 | */ -------------------------------------------------------------------------------- /9. Object-Methods/17. Object.isPrototypeOf().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.isPrototypeOf() 3 | The JavaScript Object.isPrototypeOf() method checks if an object exists in another object's prototype chain. 4 | 5 | The syntax of the isPrototypeOf() method is: 6 | 7 | prototypeObj.isPrototypeOf(object) 8 | Here, prototypeObj is an object. 9 | 10 | isPrototypeOf() Parameters 11 | The isPrototypeOf() method takes in: 12 | 13 | object - The object whose prototype chain will be searched. 14 | Return value from isPrototypeOf() 15 | Returns a Boolean indicating whether the calling object lies in the prototype chain of the specified object. 16 | Note: isPrototypeOf() differs from instanceof operator as it checks the object prototype chain against prototypeObj not prototypeObj.prototype. 17 | 18 | Example: Using Object.isPrototypeOf() 19 | let obj = new Object(); 20 | console.log(Object.prototype.isPrototypeOf(obj)); // true 21 | console.log(Function.prototype.isPrototypeOf(obj.toString)); // true 22 | 23 | console.log(Array.prototype.isPrototypeOf([2, 4, 8])); // true 24 | 25 | // define object 26 | let Animal = { 27 | makeSound() { 28 | console.log(`${this.name}, ${this.sound}!`); 29 | }, 30 | }; 31 | 32 | // new object 33 | function Dog(name) { 34 | this.name = name; 35 | this.sound = "bark"; 36 | 37 | // setting prototype using setPrototypeOf() 38 | Object.setPrototypeOf(this, Animal); 39 | } 40 | 41 | dog1 = new Dog("Marcus"); 42 | 43 | console.log(Animal.isPrototypeOf(dog1)); // true 44 | 45 | 46 | 47 | 48 | 49 | 50 | */ -------------------------------------------------------------------------------- /9. Object-Methods/18. Object.isSealed().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.isSealed() 3 | The JavaScript Object.isSealed() method checks if an object is sealed. 4 | 5 | The syntax of the isSealed() method is: 6 | 7 | Object.isSealed(obj) 8 | The isSealed() method, being a static method, is called using the Object class name. 9 | 10 | isSealed() Parameters 11 | The isSealed() method takes in: 12 | 13 | obj - The object which should be checked. 14 | Return value from isSealed() 15 | A Boolean indicating whether or not the given object is sealed. 16 | Note: An object is sealed if it is not extensible and if all its properties are non-configurable and therefore not removable (but not necessarily non-writable). 17 | 18 | Example: Using isSealed() 19 | // By default, objects are not sealed 20 | let empty = {}; 21 | console.log(Object.isSealed(empty)); // false 22 | 23 | // non-extensible empty objects are sealed 24 | Object.preventExtensions(empty); 25 | console.log(Object.isSealed(empty)); // true 26 | 27 | // for non-empty objects, all properties have to be non-configurable 28 | let obj = { value: "A B C" }; 29 | Object.preventExtensions(obj); 30 | console.log(Object.isSealed(obj)); // false 31 | 32 | // making all properties non-configurable 33 | Object.defineProperty(obj, "value", { configurable: false }); 34 | console.log(Object.isSealed(obj)); // true 35 | 36 | // Using seal() 37 | let obj2 = { a: 1, b: 2, c: 3 }; 38 | Object.seal(obj2); 39 | console.log(Object.isSealed(obj2)); // true 40 | 41 | 42 | 43 | 44 | 45 | 46 | */ -------------------------------------------------------------------------------- /9. Object-Methods/19. Object.keys().js: -------------------------------------------------------------------------------- 1 | /* 2 | The Object.keys() returns an array of a given object's own enumerable property names. 3 | 4 | Example 5 | let Student = { 6 | name: "Lisa", 7 | age: 24, 8 | marks: 78.9, 9 | }; 10 | 11 | // get all keys of Student 12 | let std1 = Object.keys(Student); 13 | console.log(std1); 14 | 15 | // Output: [ 'name', 'age', 'marks' ] 16 | 17 | 18 | 19 | 20 | 21 | 22 | */ -------------------------------------------------------------------------------- /9. Object-Methods/2. Object.create.js: -------------------------------------------------------------------------------- 1 | let a1 = { 2 | name: "Suhail", 3 | age: 20, 4 | }; 5 | 6 | let a2 = Object.create(a1); 7 | console.log(a2); // {} 8 | 9 | // But it has access to all a1 keys 10 | 11 | console.log(a2.name); // Suhail 12 | console.log(a2.age); // 20 13 | 14 | console.log(a1); 15 | 16 | // It copies whole object into new variable but not a refernce one 17 | 18 | // it shows empty when you console 19 | // but it has access to all old objects 20 | -------------------------------------------------------------------------------- /9. Object-Methods/20. Object.preventExtensions().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.preventExtensions() 3 | The JavaScript Object.preventExtensions() method prevents new properties from being added to an object. 4 | 5 | The syntax of the preventExtensions() method is: 6 | 7 | Object.preventExtensions(obj) 8 | The preventExtensions() method, being a static method, is called using the Object class name. 9 | 10 | preventExtensions() Parameters 11 | The preventExtensions() method takes in: 12 | 13 | obj - The object which should be made non-extensible. 14 | Return value from preventExtensions() 15 | Returns the object being made non-extensible. 16 | Notes: 17 | 18 | The properties of a non-extensible object, in general, may still be deleted. 19 | Attempting to add new properties to a non-extensible object will fail, either silently or by throwing a TypeError in strict mode. 20 | Properties can still be added to the non-extensible object's prototype. 21 | Example: Using preventExtensions() 22 | let obj = {}; 23 | // by default, objects are extensible 24 | console.log(Object.isExtensible(obj)); // true 25 | 26 | let obj1 = Object.preventExtensions(obj); 27 | console.log(obj == obj1); // true -> Returns the same object 28 | 29 | console.log(Object.isExtensible(obj)); // false 30 | 31 | // TypeError: Cannot define property newProp, object is not extensible 32 | Object.defineProperty(obj, "newProp", { 33 | value: 123456, 34 | }); 35 | 36 | 37 | 38 | 39 | 40 | 41 | */ -------------------------------------------------------------------------------- /9. Object-Methods/21. Object.propertyIsEnumerable().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.propertyIsEnumerable() 3 | The JavaScript Object.propertyIsEnumerable() method checks if the given property is enumerable and is the object's own property. 4 | 5 | The syntax of the propertyIsEnumerable() method is: 6 | 7 | obj.propertyIsEnumerable(prop) 8 | Here, obj is an object. 9 | 10 | propertyIsEnumerable() Parameters 11 | The propertyIsEnumerable() method takes in: 12 | 13 | prop - The name of the property to test. 14 | Return value from propertyIsEnumerable() 15 | Returns a Boolean indicating whether the specified property is enumerable and is the object's own property. 16 | Note: Every object has a propertyIsEnumerable method. This method can determine whether the specified property in an object can be enumerated by a for...in loop. 17 | 18 | Example: Using Object.propertyIsEnumerable() 19 | let arr = [1, 2, 3, 4]; 20 | console.log(arr.propertyIsEnumerable(0)); // true 21 | console.log(arr.propertyIsEnumerable("length")); // false 22 | 23 | console.log(Math.propertyIsEnumerable("random")); // false 24 | 25 | let obj = { 26 | prop: "Hello World!", 27 | }; 28 | console.log(obj.propertyIsEnumerable("prop")); // true 29 | // property does not exist 30 | console.log(obj.propertyIsEnumerable("random")); // false 31 | 32 | 33 | 34 | 35 | 36 | 37 | */ -------------------------------------------------------------------------------- /9. Object-Methods/22. Object.seal().js: -------------------------------------------------------------------------------- 1 | /* 2 | avascript Object.seal() 3 | The JavaScript Object.seal() method seals the given object. 4 | 5 | The seal() method prevents new properties from being added to the object and marks all the existing properties as non-configurable. 6 | 7 | The syntax of the seal() method is: 8 | 9 | Object.seal(obj) 10 | The seal() method, being a static method, is called using the Object class name. 11 | 12 | seal() Parameters 13 | The seal() method takes in: 14 | 15 | obj - The object that is to be sealed. 16 | Return value from seal() 17 | Returns the object being sealed. 18 | Example: Using seal() 19 | let obj = { 20 | foo: "bar", 21 | func: function () {}, 22 | }; 23 | 24 | // before sealing, properties can be added, modified, or removed 25 | obj.foo = "JavaScript"; 26 | obj.value = 5; 27 | delete obj.func; 28 | 29 | // sealing the object 30 | o = Object.seal(obj); 31 | 32 | // can still change property values 33 | obj.foo = "bar1"; 34 | 35 | // no other change 36 | // fails silently 37 | obj.foo1 = "bar"; 38 | delete obj.foo; 39 | 40 | console.log(obj); // { foo: 'bar1', value: 5 } 41 | 42 | // cannot convert data property to accessors or vice versa 43 | Object.defineProperty(obj, "foo", { 44 | get: function () { 45 | return "g"; 46 | }, 47 | 48 | }): 49 | 50 | 51 | 52 | 53 | 54 | 55 | */ 56 | -------------------------------------------------------------------------------- /9. Object-Methods/23. Object.setPrototypeOf().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.setPrototypeOf() 3 | The JavaScript Object.setPrototypeOf() method sets the prototype of a specified object to another object or null. 4 | 5 | The syntax of the setPrototypeOf() method is: 6 | 7 | Object.setPrototypeOf(obj, prototype) 8 | The setPrototypeOf() method, being a static method, is called using the Object class name. 9 | 10 | setPrototypeOf() Parameters 11 | The setPrototypeOf() method takes in: 12 | 13 | obj - The object which is to have its prototype set. 14 | prototype - The object's new prototype (an object or null). 15 | Return value from setPrototypeOf() 16 | Returns the specified object. 17 | Note: Changing the [[Prototype]] of an object is currently a very slow operation in every browser and JavaScript engine. 18 | 19 | Example 1: Using Object.setPrototypeOf() 20 | let Animal = { 21 | makeSound() { 22 | console.log(`${this.name}, ${this.sound}!`); 23 | }, 24 | }; 25 | // defining new Dog object 26 | function Dog(name) { 27 | this.name = name; 28 | this.sound = "bark"; 29 | // setting prototype to Animal 30 | Object.setPrototypeOf(this, Animal); 31 | } 32 | 33 | dog1 = new Dog("Marcus"); 34 | 35 | dog1.makeSound(); // Marcus, bark! 36 | 37 | 38 | 39 | 40 | 41 | 42 | */ -------------------------------------------------------------------------------- /9. Object-Methods/24. Object toLocaleString().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object toLocaleString() 3 | The JavaScript Object toLocaleString() method returns a string representing the object. 4 | 5 | The syntax of the toLocaleString() method is: 6 | 7 | obj.toLocaleString() 8 | Here, obj is an object. 9 | 10 | toLocaleString() Parameters 11 | The toLocaleString() method does not take in any parameters by default. 12 | 13 | Return value from toLocaleString() 14 | Returns a string representing the object. 15 | Some objects overriding toLocaleString: 16 | 17 | Array: JavaScript Array toLocaleString() 18 | Number: JavaScript Number toLocaleString() 19 | Example: Using toLocaleString() method 20 | // toLocaleString for Array 21 | const arr = [4, 7, 10]; 22 | let string = arr.toLocaleString("fr", { style: "currency", currency: "EUR" }); 23 | console.log(string); // 4,00 €,7,00 €,10,00 € 24 | 25 | // toLocaleString for Number 26 | const num = 123456.789; 27 | // India uses thousands/lakh/crore separators 28 | console.log(num.toLocaleString("en-IN")); // 1,23,456.789 29 | 30 | // toLocaleString for Date 31 | const date = new Date(Date.now()); 32 | console.log(date); // Wed Jul 29 2020 15:37:00 GMT+0545 (Nepal Time) 33 | 34 | let dateStr = date.toLocaleString("de"); 35 | console.log(dateStr); // 29.7.2020, 15:37:00 36 | 37 | 38 | 39 | 40 | 41 | 42 | */ -------------------------------------------------------------------------------- /9. Object-Methods/25. Object toString().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object toString() 3 | The JavaScript Object toString() method returns the object as a string. 4 | 5 | The syntax of the toString() method is: 6 | 7 | obj.toString() 8 | Here, obj is an object. 9 | 10 | toString() Parameters 11 | The toString() method does not take any parameters. 12 | 13 | Return value from toString() 14 | Returns a string representing the object. 15 | Note: Every object descended from Object inherits toString() and if not overridden, it returns "[object ]". 16 | 17 | Example: Using toString() 18 | // built-in objects 19 | let num = 10; 20 | // number takes in optional radix argument (numeral base) 21 | console.log(num.toString(2)); // "1010" in binary 22 | 23 | console.log(new Date().toString()); // Thu Aug 06 2020 12:08:44 GMT+0545 (Nepal Time) 24 | 25 | // overriding default toString(), custom object 26 | function Dog(name, breed, sex) { 27 | this.name = name; 28 | this.breed = breed; 29 | this.sex = sex; 30 | } 31 | 32 | dog1 = new Dog("Daniel", "bulldog", "male"); 33 | console.log(dog1.toString()); // [object Object] 34 | 35 | Dog.prototype.toString = function dogToString() { 36 | return `${this.name} is a ${this.sex} ${this.breed}.`; 37 | }; 38 | 39 | console.log(dog1.toString()); // Daniel is a male bulldog. 40 | 41 | 42 | 43 | 44 | 45 | 46 | */ -------------------------------------------------------------------------------- /9. Object-Methods/26. Object valueOf().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object valueOf() 3 | The JavaScript Object valueOf() method returns the primitive value of the specified object. 4 | 5 | The syntax of the valueOf() method is: 6 | 7 | obj.valueOf() 8 | Here, obj is an object. 9 | 10 | valueOf() Parameters 11 | The valueOf() method does not take any parameters. 12 | 13 | Return value from valueOf() 14 | Returns the primitive value of the specified object. 15 | Notes: 16 | 17 | For objects of type Object, there is no primitive value, so valueOf() method simply returns the object itself. 18 | For objects of type Number, Boolean, or String, however, valueOf() returns the primitive value represented by the corresponding object. 19 | Example 1: Custom valueOf() 20 | function customNum(n, fact) { 21 | this.number = n; 22 | this.fact = fact; 23 | } 24 | 25 | customNum.prototype.valueOf = function () { 26 | return this.number; 27 | }; 28 | 29 | var num1 = new customNum(2, "First Prime Number"); 30 | 31 | console.log(num1 + 3); // 5 32 | 33 | 34 | 35 | 36 | 37 | 38 | */ -------------------------------------------------------------------------------- /9. Object-Methods/27. Object.values().js: -------------------------------------------------------------------------------- 1 | // Array objects 2 | const arr = ["JavaScript", "Python", "C"]; 3 | console.log(Object.values(arr)); // [ 'JavaScript', 'Python', 'C' ] 4 | 5 | // Array-like objects 6 | const obj = { 65: "A", 66: "B", 67: "C" }; 7 | console.log(Object.values(obj)); // [ 'A', 'B', 'C' ] 8 | 9 | // random key ordering 10 | const obj1 = { 42: "a", 22: "b", 71: "c" }; 11 | console.log(Object.values(obj1)); // ['b', 'a', 'c'] -> Arranged in key's numerical order 12 | 13 | // string -> from ES2015+, non objects are coerced to object 14 | const string = "code"; 15 | console.log(Object.values(string)); // [ 'c', 'o', 'd', 'e' ] 16 | -------------------------------------------------------------------------------- /9. Object-Methods/3. Object.defineProperties().js: -------------------------------------------------------------------------------- 1 | let obj = {}; 2 | 3 | Object.defineProperties(obj, { 4 | property1: { 5 | value: true, 6 | writable: true, 7 | }, 8 | property2: { 9 | value: "Hello", 10 | writable: false, 11 | }, 12 | }); 13 | 14 | console.log(obj); // {} 15 | console.log(obj.property1); // true 16 | console.log(obj.property2); // Hello 17 | 18 | // Adding key value pair oin differnet style 19 | // if u console object it will be empty 20 | 21 | // if u console object with key it will be return value of it 22 | -------------------------------------------------------------------------------- /9. Object-Methods/4. Object.defineProperty().js: -------------------------------------------------------------------------------- 1 | let obj = {}; 2 | 3 | // with a data property descriptor 4 | Object.defineProperty(obj, "property1", { 5 | value: 788, 6 | writable: true, 7 | enumerable: true, 8 | configurable: true, 9 | }); 10 | 11 | console.log(obj.property1); // 788 12 | 13 | // with an accessor property descriptor 14 | Object.defineProperty(obj, "property2", { 15 | get() { 16 | console.log("Getting Value..."); 17 | return val; 18 | }, 19 | set(newVal) { 20 | console.log("Setting Value..."); 21 | val = newVal; 22 | }, 23 | enumerable: true, 24 | configurable: true, 25 | }); 26 | 27 | obj.property2 = 6969; // Setting Value... 28 | 29 | console.log(obj.property2); // 6969 30 | 31 | 32 | -------------------------------------------------------------------------------- /9. Object-Methods/5. Object.entries().js: -------------------------------------------------------------------------------- 1 | const obj = { name: "Adam", age: 20, location: "Nepal" }; 2 | console.log(Object.entries(obj)); // [ [ 'name', 'Adam' ], [ 'age', 20 ], [ 'location', 'Nepal' ] ] 3 | 4 | // Array-like objects 5 | const obj1 = { 0: "A", 1: "B", 2: "C" }; 6 | console.log(Object.entries(obj1)); // [ [ '0', 'A' ], [ '1', 'B' ], [ '2', 'C' ] ] 7 | 8 | // random key ordering 9 | const obj2 = { 42: "a", 22: "b", 71: "c" }; 10 | // [ [ '22', 'b' ], [ '42', 'a' ], [ '71', 'c' ] ] -> arranged in numerical order of keys 11 | console.log(Object.entries(obj2)); 12 | 13 | // string -> from ES2015+, non objects are coerced to object 14 | const string = "code"; 15 | console.log(Object.entries(string)); // [ [ '0', 'c' ], [ '1', 'o' ], [ '2', 'd' ], [ '3', 'e' ] ] 16 | 17 | // primite types have no properties 18 | console.log(Object.entries(55)); // [] 19 | 20 | // Iterating through key-value of objects 21 | for (const [key, value] of Object.entries(obj)) { 22 | console.log(`${key}: ${value}`); 23 | } 24 | -------------------------------------------------------------------------------- /9. Object-Methods/6. Object.freeze().js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Object.freeze() 3 | The JavaScript Object.freeze() method freezes an object. 4 | 5 | A frozen object can no longer be changed. Freezing an object prevents: 6 | 7 | New properties from being added to the object. 8 | Existing properties to be removed from the object. 9 | Changing the enumerability, configurability, or writability of existing properties. 10 | Changing values of the existing object properties and prototype. 11 | The syntax of the freeze() method is: 12 | 13 | Object.freeze(obj) 14 | The freeze() method, being a static method, is called using the Object class name. 15 | 16 | freeze() Parameters 17 | The freeze() method takes in: 18 | 19 | obj - The object to freeze. 20 | Return value from freeze() 21 | The object that was passed to the function. 22 | Notes: 23 | 24 | Any attempts to add or remove from properties set of frozen objects will fail either silently or throw TypeError (mostly when in strict mode). 25 | Objects sealed with Object.seal() can have their existing properties changed while Object.freeze() makes the existing properties immutable. 26 | Example: Using freeze() 27 | let obj = { 28 | prop: function () {}, 29 | foo: "bar", 30 | }; 31 | 32 | let o = Object.freeze(obj); 33 | 34 | console.log(o == obj); // True -> Returns the same object 35 | console.log(Object.isFrozen(obj)); // true 36 | 37 | // changes will fail silently 38 | obj.foo = "bar1"; 39 | console.log(obj.foo); // bar 40 | 41 | // does not add silently 42 | obj.new_foo = "bar"; 43 | console.log(obj.new_foo); // undefined 44 | 45 | // Following will also throw error 46 | // Object.setPrototypeOf(obj, { x: 20 }) 47 | 48 | 49 | 50 | 51 | 52 | 53 | */ -------------------------------------------------------------------------------- /9. Object-Methods/7. Object.fromEntries().js: -------------------------------------------------------------------------------- 1 | /* 2 | Javascript Object.fromEntries() 3 | The JavaScript Object.fromEntries() method creates an object from a list of key-value pairs. 4 | 5 | The syntax of the fromEntries() method is: 6 | 7 | Object.fromEntries(iterable) 8 | The fromEntries() method, being a static method, is called using the Object class name. 9 | 10 | fromEntries() Parameters 11 | The fromEntries() method takes in: 12 | 13 | iterable - An iterable such as Array or Map or other objects implementing the iterable protocol. 14 | Return value from fromEntries() 15 | Returns a new object whose properties are given by the entries of the iterable. 16 | Note: Object.fromEntries() performs the reverse of Object.entries(). 17 | 18 | Example: Using fromEntries() 19 | // Converting Map to an Object 20 | const map = new Map([ 21 | ["a", 1], 22 | ["b", 2], 23 | ]); 24 | const obj = Object.fromEntries(map); 25 | console.log(obj); // { a: 1, b: 2 } 26 | 27 | // Converting Map to an Object 28 | const arr = [ 29 | ["0", "a"], 30 | ["1", "b"], 31 | ["2", "c"], 32 | ]; 33 | const newObj = Object.fromEntries(arr); 34 | console.log(newObj); // { '0': 'a', '1': 'b', '2': 'c' } 35 | 36 | 37 | 38 | 39 | 40 | 41 | */ -------------------------------------------------------------------------------- /9. Object-Methods/8. Object getOwnPropertyDescriptor().js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Object getOwnPropertyDescriptor() 3 | The JavaScript Object.getOwnPropertyDescriptor() method returns a property descriptor for an object's own property. 4 | 5 | The syntax of the getOwnPropertyDescriptor() method is: 6 | 7 | Object.getOwnPropertyDescriptor(obj, prop) 8 | The getOwnPropertyDescriptor() method, being a static method, is called using the Object class name. 9 | 10 | getOwnPropertyDescriptor() Parameters 11 | The getOwnPropertyDescriptor() method takes in: 12 | 13 | obj - The object in which to look for the property. 14 | prop - The name or Symbol of the property whose description is to be retrieved. 15 | Return value from getOwnPropertyDescriptor() 16 | Returns a property descriptor of the given property on the object. 17 | Returns undefined if the property does not exist on the object. 18 | Example: Using getOwnPropertyDescriptor() 19 | let obj = { 20 | x: 10, 21 | get number() { 22 | return this.x; 23 | }, 24 | }; 25 | 26 | let xValue = Object.getOwnPropertyDescriptor(obj, "x"); 27 | console.log(xValue); 28 | 29 | let value = Object.getOwnPropertyDescriptor(obj, "number"); 30 | console.log(value); 31 | 32 | Object.defineProperty(obj, "name", { 33 | value: "JavaScript", 34 | writable: false, 35 | enumerable: false, 36 | }); 37 | 38 | console.log(Object.getOwnPropertyDescriptor(obj, "name")); 39 | 40 | 41 | 42 | 43 | 44 | 45 | */ -------------------------------------------------------------------------------- /9. Object-Methods/9. Object getOwnPropertyDescriptors().js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Object getOwnPropertyDescriptors() 3 | The JavaScript Object.getOwnPropertyDescriptors() method returns all own property descriptors of a given object. 4 | 5 | The syntax of the getOwnPropertyDescriptors() method is: 6 | 7 | Object.getOwnPropertyDescriptors(obj) 8 | The getOwnPropertyDescriptors() method, being a static method, is called using the Object class name. 9 | 10 | getOwnPropertyDescriptors() Parameters 11 | The getOwnPropertyDescriptors() method takes in: 12 | 13 | obj - The object for which to get all own property descriptors. 14 | Return value from getOwnPropertyDescriptors() 15 | Returns an object containing all own property descriptors of an object 16 | Example: Using getOwnPropertyDescriptors() 17 | let obj = { 18 | x: 10, 19 | get number() { 20 | return this.x; 21 | }, 22 | }; 23 | 24 | let value = Object.getOwnPropertyDescriptors(obj); 25 | console.log(value); 26 | 27 | // getOwnPropertyDescriptors() can be used for shallow clone 28 | let cloneObj = Object.create( 29 | Object.getPrototypeOf(obj), 30 | Object.getOwnPropertyDescriptors(obj) 31 | ); 32 | 33 | console.log(cloneObj); // { x: 10, number: [Getter] } 34 | Run Code 35 | Output 36 | 37 | { 38 | x: { value: 10, writable: true, enumerable: true, configurable: true }, 39 | number: { 40 | get: [Function: get number], 41 | set: undefined, 42 | enumerable: true, 43 | configurable: true 44 | } 45 | } 46 | { x: 10, number: [Getter] } 47 | 48 | 49 | 50 | 51 | 52 | 53 | */ -------------------------------------------------------------------------------- /9. Object-Methods/Important.txt: -------------------------------------------------------------------------------- 1 | Link : https://blog.devgenius.io/ultimate-js-object-methods-cheatsheet-2022-5adc88cdc074 2 | 3 | 4 | Managing Objects 5 | // Create object with an existing object as prototype 6 | Object.create() 7 | 8 | // Adding or changing an object property 9 | Object.defineProperty(object, property, descriptor) 10 | 11 | // Adding or changing object properties 12 | Object.defineProperties(object, descriptors) 13 | 14 | // Accessing Properties 15 | Object.getOwnPropertyDescriptor(object, property) 16 | 17 | // Returns all properties as an array 18 | Object.getOwnPropertyNames(object) 19 | 20 | // Accessing the prototype 21 | Object.getPrototypeOf(object) 22 | 23 | // Returns enumerable properties as an array 24 | Object.keys(object) 25 | 26 | Protecting Objects 27 | // Prevents adding properties to an object 28 | Object.preventExtensions(object) 29 | 30 | // Returns true if properties can be added to an object 31 | Object.isExtensible(object) 32 | 33 | // Prevents changes of object properties (not values) 34 | Object.seal(object) 35 | 36 | // Returns true if object is sealed 37 | Object.isSealed(object) 38 | 39 | // Prevents any changes to an object 40 | Object.freeze(object) 41 | 42 | // Returns true if object is frozen 43 | Object.isFrozen(object) 44 | 45 | 46 | //The method Object.values() allows you to get all the values inside an 47 | object as an array. You just have to pass the object as an argument to 48 | the method Object.values() . 49 | Object.values(object) 50 | 51 | 52 | // The method Object.entries() is also useful. 53 | It allows you to get both the keys and values 54 | of an object and it returns a multidimensional 55 | array that contains other arrays for each key and value. 56 | Object entries -------------------------------------------------------------------------------- /Advanced-JavaScript/10. Function-Methods/1. apply().js: -------------------------------------------------------------------------------- 1 | // function.apply 2 | 3 | const obj = { 4 | name: "Suhail", 5 | age: 20, 6 | call() { 7 | return `Name is ${this.name} and age is ${this.age}`; 8 | }, 9 | }; 10 | 11 | const obj2 = { 12 | name: "Roushan", 13 | age: 22, 14 | }; 15 | 16 | let data = obj.call.apply(obj2) 17 | console.log(data); 18 | 19 | -------------------------------------------------------------------------------- /Advanced-JavaScript/10. Function-Methods/2. bind().js: -------------------------------------------------------------------------------- 1 | const first = { 2 | name: "Suhail", 3 | age: 20, 4 | call() { 5 | return `${this.name} and ${this.age}`; 6 | }, 7 | }; 8 | 9 | const second = { 10 | name: "Suhail", 11 | age: 20, 12 | }; 13 | 14 | let data = first.call.bind(second); 15 | console.log(data()); 16 | -------------------------------------------------------------------------------- /Advanced-JavaScript/10. Function-Methods/3. call().js: -------------------------------------------------------------------------------- 1 | const call = () => `Hello I am Suhail`; 2 | 3 | let data = call.call(); 4 | console.log(data); 5 | -------------------------------------------------------------------------------- /Advanced-JavaScript/10. Function-Methods/4. name().js: -------------------------------------------------------------------------------- 1 | function name() { 2 | console.log("I am Function Name "); 3 | } 4 | 5 | let data = name.name; 6 | console.log(data); 7 | -------------------------------------------------------------------------------- /Advanced-JavaScript/10. Function-Methods/5. length.js: -------------------------------------------------------------------------------- 1 | function number(a, b, c) { 2 | console.log(a, b, c); 3 | } 4 | 5 | let data = number.length; 6 | 7 | console.log(data); // 3 8 | 9 | function odd(a, b = 10, c) { 10 | console.log(a, b, c); 11 | } 12 | 13 | data = odd.length; 14 | 15 | console.log(data); // 1 16 | 17 | /* 18 | a=11,b,c ===> 0 19 | a,b=11,c ===> 1 20 | a,b,c=10 ===> 2 21 | 22 | // It always count from first 23 | 24 | */ 25 | -------------------------------------------------------------------------------- /Advanced-JavaScript/10. Function-Methods/6. toString().js: -------------------------------------------------------------------------------- 1 | function Suhail() { 2 | return "Hey Everyone"; 3 | } 4 | 5 | console.log(Suhail.toString()); 6 | 7 | 8 | 9 | // Output:- 10 | // function Suhail() { 11 | // return "Hey Everyone"; 12 | // } -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/1. isFinite().js: -------------------------------------------------------------------------------- 1 | let number = 775; 2 | console.log(isFinite(number)); // true 3 | 4 | let number2 = "745533e3"; 5 | console.log(isFinite(number2)); // true 6 | 7 | let number3 = 745533e3; 8 | console.log(isFinite(number3)); // true 9 | console.log(isFinite(null)); // true 10 | 11 | console.log(isFinite(NaN)); // false 12 | console.log(isFinite(-Infinity)); // false 13 | console.log(isFinite(+Infinity)); // false 14 | console.log(isFinite(undefined)); // false 15 | -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/2. isInteger().js: -------------------------------------------------------------------------------- 1 | console.log(Number.isInteger(123)); // true 2 | console.log(Number.isInteger(3473.345)); // false 3 | console.log(Number.isInteger("873")); // false 4 | console.log(Number.isInteger(1e1)); // true 5 | 6 | console.log(Number.isInteger(5.0)); // true; 5.0 == 5 7 | console.log(Number.isInteger(0.5)); // false 0.5 8 | 9 | -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/3. parseFloat().js: -------------------------------------------------------------------------------- 1 | // String to Number 2 | 3 | let data = "123"; 4 | let change = Number.parseFloat(data); 5 | console.log(typeof change,change); 6 | 7 | let data2 = "123e4"; 8 | change = Number.parseFloat(data2); 9 | console.log(typeof change, change); 10 | 11 | 12 | let data3 = "d234eNde"; 13 | change = Number.parseFloat(data3); 14 | console.log(typeof change, change); 15 | 16 | 17 | let data4 = "234eNde"; 18 | change = Number.parseFloat(data4); 19 | console.log(typeof change, change); 20 | -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/4. parseInt().js: -------------------------------------------------------------------------------- 1 | let number = 0000; 2 | console.log(parseInt(number,2)); 3 | 4 | 5 | number = 0001; 6 | console.log(parseInt(number,2)); 7 | 8 | number = " 0xF" 9 | console.log(parseInt(number, 16)); 10 | -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/5. isNan().js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/Advanced-JavaScript/11. Number-Methods/5. isNan().js -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/6. isSafeInteger().js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/Advanced-JavaScript/11. Number-Methods/6. isSafeInteger().js -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/7. toExponential().js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/Advanced-JavaScript/11. Number-Methods/7. toExponential().js -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/8. toFixed().js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/Advanced-JavaScript/11. Number-Methods/8. toFixed().js -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/9. toExponential().js: -------------------------------------------------------------------------------- 1 | let number = 10; 2 | 3 | console.log(number.toExponential(1)); 4 | // JavaScript Number toExponential() 5 | // The JavaScript Number toExponential() method returns a string representing the Number object in exponential notation. 6 | 7 | // The syntax of the toExponential() method is: 8 | 9 | // num.toExponential(fractionDigits) 10 | // Here, num is a number. 11 | 12 | // Number toExponential() Parameters 13 | // The toExponential() method takes in: 14 | // 15 | // fractionDigits (Optional) - An integer specifying the number of digits after the decimal point. By default, it is as many digits as necessary to specify the number. 16 | // Return value from Number toExponential() 17 | // Returns a string representing the given Number object in exponential notation, rounded to fractionDigits digits after the decimal point. 18 | // Example: Using Number.toExponential() 19 | let num = 695.8457; 20 | 21 | let exp_num = num.toExponential(); 22 | console.log(exp_num); // 6.958457e+2 23 | 24 | let exp_num1 = num.toExponential(2); 25 | console.log(exp_num1); // 6.96e+2 26 | // Run Code 27 | // Output 28 | 29 | 6.958457e+2 30 | 6.96e+2 -------------------------------------------------------------------------------- /Advanced-JavaScript/11. Number-Methods/Other.js: -------------------------------------------------------------------------------- 1 | console.log(Number.MIN_SAFE_INTEGER); 2 | 3 | console.log(Number.MAX_SAFE_INTEGER); 4 | console.log(Number.MIN_VALUE); 5 | console.log(Number.MAX_VALUE); 6 | 7 | console.log(Number.EPSILON); // 2.220446049250313e-16 8 | 9 | console.log(Number.NEGATIVE_INFINITY); 10 | console.log(Number.POSITIVE_INFINITY); 11 | console.log(Number.NaN); 12 | 13 | /* 14 | 15 | -9007199254740991 16 | 9007199254740991 17 | 5e-324 18 | 1.7976931348623157e+308 19 | 2.220446049250313e-16 20 | -Infinity 21 | Infinity 22 | NaN 23 | 24 | */ 25 | -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/1.js: -------------------------------------------------------------------------------- 1 | // JavaScript Symbol 2 | // The JavaScript ES6 introduced a new primitive data type called Symbol. Symbols are immutable (cannot be changed) and are unique. For example, 3 | 4 | // two symbols with the same description 5 | 6 | const value1 = Symbol('hello'); 7 | const value2 = Symbol('hello'); 8 | 9 | console.log(value1 === value2); // false 10 | // Though value1 and value2 both contain the same description, they are different. -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/2.js: -------------------------------------------------------------------------------- 1 | // Creating Symbol 2 | // You use the Symbol() function to create a Symbol. For example, 3 | 4 | // creating symbol 5 | const x = Symbol(); 6 | 7 | console.log(typeof x); // symbol 8 | // You can pass an optional string as its description. For example, 9 | 10 | const x1 = Symbol("hey"); 11 | console.log(x1); // Symbol(hey) 12 | -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/3.js: -------------------------------------------------------------------------------- 1 | // Access Symbol Description 2 | // To access the description of a symbol, we use the . operator. For example, 3 | 4 | const x = Symbol('hey'); 5 | console.log(x.description); // hey 6 | -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/4.js: -------------------------------------------------------------------------------- 1 | // Add Symbol as an Object Key 2 | // You can add symbols as a key in an object using square brackets []. For example, 3 | 4 | let id = Symbol("id"); 5 | 6 | let person = { 7 | name: "Jack", 8 | 9 | // adding symbol as a key 10 | [id]: 123 // not "id": 123 11 | }; 12 | 13 | console.log(person); // {name: "Jack", Symbol(id): 123} -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/5.js: -------------------------------------------------------------------------------- 1 | // Symbols are not included in for...in Loop 2 | // The for...in loop does not iterate over Symbolic properties. For example, 3 | 4 | let id = Symbol("id"); 5 | 6 | let person = { 7 | name: "Jack", 8 | age: 25, 9 | [id]: 12 10 | }; 11 | 12 | // using for...in 13 | for (let key in person) { 14 | console.log(key); 15 | } -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/6.js: -------------------------------------------------------------------------------- 1 | // Benefit of Using Symbols in Object 2 | // If the same code snippet is used in various programs, then it is better to use Symbols in the object key. It's because you can use the same key name in different codes and avoid duplication issues. For example, 3 | 4 | let person = { 5 | name: "Jack" 6 | }; 7 | 8 | // creating Symbol 9 | let id = Symbol("id"); 10 | 11 | // adding symbol as a key 12 | person[id] = 12; 13 | // In the above program, if the person object is also used by another program, then you wouldn't want to add a property that can be accessed or changed by another program. Hence by using Symbol, you create a unique property that you can use. 14 | 15 | // Now, if the other program also needs to use a property named id, just add a Symbol named id and there won't be duplication issues. For example, 16 | person = { 17 | name: "Jack" 18 | }; 19 | 20 | let id1 = Symbol("id"); 21 | 22 | person[id1] = "Another value"; 23 | // In the above program, even if the same name is used to store values, the Symbol data type will have a unique value. 24 | 25 | // In the above program, if the string key was used, then the later program would have changed the value of the property. For example, 26 | 27 | person = { 28 | name: "Jack" 29 | }; 30 | 31 | // using string as key 32 | person.id = 12; 33 | console.log(person.id); // 12 34 | 35 | // Another program overwrites value 36 | person.id = 'Another value'; 37 | console.log(person.id); // Another value 38 | // In the above program, the second user.id overwrites the previous value. 39 | 40 | -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/7.js: -------------------------------------------------------------------------------- 1 | // Symbol Methods 2 | // There are various methods available with Symbol. 3 | 4 | // Method Description 5 | // for() Searches for existing symbols 6 | // keyFor() Returns a shared symbol key from the global symbol registry. 7 | // toSource() Returns a string containing the source of the Symbol object 8 | // toString() Returns a string containing the description of the Symbol 9 | // valueOf() Returns the primitive value of the Symbol object. 10 | // Example: Symbol Methods 11 | // get symbol by name 12 | let sym = Symbol.for('hello'); 13 | let sym1 = Symbol.for('id'); 14 | 15 | // get name by symbol 16 | console.log( Symbol.keyFor(sym) ); // hello 17 | console.log( Symbol.keyFor(sym1) ); // id -------------------------------------------------------------------------------- /Advanced-JavaScript/12. Symbol in JavaScript/8.js: -------------------------------------------------------------------------------- 1 | // Symbol Properties 2 | // Properties Description 3 | // asyncIterator Returns the default AsyncIterator for an object 4 | // hasInstance Determines if a constructor object recognizes an object as its instance 5 | // isConcatSpreadable Indicates if an object should be flattened to its array elements 6 | // iterator Returns the default iterator for an object 7 | // match Matches against a string 8 | // matchAll Returns an iterator that yields matches of the regular expression against a string 9 | // replace Replaces matched substrings of a string 10 | // search Returns the index within a string that matches the regular expression 11 | // split Splits a string at the indices that match a regular expression 12 | // species Creates derived objects 13 | // toPrimitive Converts an object to a primitive value 14 | // toStringTag Gives the default description of an object 15 | // description Returns a string containing the description of the symbol 16 | // Example: Symbol Properties Example 17 | const x = Symbol('hey'); 18 | 19 | // description property 20 | console.log(x.description); // hey 21 | 22 | const stringArray = ['a', 'b', 'c']; 23 | const numberArray = [1, 2, 3]; 24 | 25 | // isConcatSpreadable property 26 | numberArray[Symbol.isConcatSpreadable] = false; 27 | 28 | let result = stringArray.concat(numberArray); 29 | console.log(result); // ["a", "b", "c", [1, 2, 3]] -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/1. Intro.js: -------------------------------------------------------------------------------- 1 | /* 2 | The try, catch and finally blocks are used to handle exceptions (a type of an error). Before you learn about them, you need to know about the types of errors in programming. 3 | 4 | Types of Errors 5 | In programming, there can be two types of errors in the code: 6 | 7 | Syntax Error: Error in the syntax. For example, if you write consol.log('your result');, the above program throws a syntax error. The spelling of console is a mistake in the above code. 8 | 9 | Runtime Error: This type of error occurs during the execution of the program. For example, 10 | calling an invalid function or a variable. 11 | 12 | 13 | These errors that occur during runtime are called exceptions. Now, let's see how you can handle these exceptions 14 | 15 | */ 16 | 17 | 18 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/2. try...catch.js: -------------------------------------------------------------------------------- 1 | // try ....catch 2 | 3 | let a = 10; 4 | let b = 20; 5 | try { 6 | console.log(a + b); 7 | console.log(c); // Error ..go to catch block 8 | } catch (error) { 9 | console.log(`Error is : ${error}`); // Error is : ReferenceError: c is not defined 10 | } 11 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/3. try..catch..finally.js: -------------------------------------------------------------------------------- 1 | // try...catch..finally 2 | 3 | var a = 10; 4 | var b = 20; 5 | 6 | try { 7 | console.log(a + b); 8 | console.log(c); 9 | } catch (error) { 10 | console.log(`Error is : ${error}`); 11 | } finally { 12 | console.log("I Continue AnyHow the Code is "); 13 | } 14 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/4. other.js: -------------------------------------------------------------------------------- 1 | // setTime in try..catch 2 | 3 | try { 4 | setTimeout(() => { 5 | console.log("Hello"); 6 | console.log(c); 7 | }, 4000); 8 | } catch (error) { 9 | console.log(`Error is : ${error}`); 10 | } 11 | // Output:- 12 | // ReferenceError: c is not defined 13 | // at Timeout._onTimeout 14 | // (/home/suhail/u-noob-learn-javascript/Advanced-JavaScript/13. Exceptions and Modules/4. other.js:6:17) 15 | // at listOnTimeout (node:internal/timers:559:17) 16 | // at processTimers (node:internal/timers:502:7) 17 | 18 | setTimeout(function () { 19 | try { 20 | console.log("Hello"); 21 | console.log(c); 22 | } catch (error) { 23 | console.log(`Error is : ${error}`); 24 | } 25 | }, 4000); 26 | 27 | // Output:- 28 | // Hello 29 | // Error is : ReferenceError: c is not defined 30 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/5 throw.js: -------------------------------------------------------------------------------- 1 | // throw 2 | 3 | let num = 3; 4 | let find = 3; 5 | try { 6 | if (num == find) throw "Milgaaya"; 7 | if (num < num / 2) throw "Less Than"; 8 | if (num > num / 2) throw "Greater Than"; 9 | } catch (error) { 10 | console.log(error); 11 | } 12 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/6. modules1.js: -------------------------------------------------------------------------------- 1 | export let call = (name) => { 2 | return `Hello My Name is ${name}`; 3 | }; 4 | 5 | export function calls(name) { 6 | return `My Name is ${name}`; 7 | } 8 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/7. modules2.js: -------------------------------------------------------------------------------- 1 | import { call as Suhail} from "./6. modules1.js"; 2 | 3 | let update = Suhail("Suhail"); 4 | console.log(update); 5 | 6 | // let update1 = calls("Roushan"); 7 | // console.log(update1); 8 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/8. modules3.js: -------------------------------------------------------------------------------- 1 | function sum(name) { 2 | return `Helllo ${name}`; 3 | } 4 | 5 | export default sum; 6 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/9. modules4.js: -------------------------------------------------------------------------------- 1 | import suhail from "./8. modules3.js"; 2 | 3 | console.log(suhail("Yo")); 4 | -------------------------------------------------------------------------------- /Advanced-JavaScript/13. Exceptions and Modules/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "13.-exceptions-and-modules", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "1. Intro.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC" 13 | } -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/1.js: -------------------------------------------------------------------------------- 1 | // JavaScript ES6 (also known as ECMAScript 2015 or ECMAScript 6) 2 | // is the newer version of JavaScript that was introduced in 2015. 3 | 4 | // ECMAScript is the standard that JavaScript programming language uses. 5 | // ECMAScript provides the specification on how JavaScript programming language should work. 6 | 7 | // This tutorial provides a brief summary of commonly used features 8 | // of ES6 so that you can start quickly in ES6. 9 | 10 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/13.js: -------------------------------------------------------------------------------- 1 | // Class Inheritance 2 | 3 | class Person { 4 | constructor(name, age) { 5 | this.name = "Suhail"; 6 | this.age = 20; 7 | } 8 | } 9 | 10 | class Person1 extends Person { 11 | constructor(isAlive) { 12 | super(isAlive); 13 | this.isAlive = true; 14 | } 15 | } 16 | 17 | let p = new Person1(); 18 | 19 | console.log(p); // Person1 { name: 'Suhail', age: 20 } 20 | 21 | console.log(p.name, p.age,p.isAlive); 22 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/15.js: -------------------------------------------------------------------------------- 1 | // JavaScript Proxy 2 | 3 | let student = { name: "Suhail", age: 23 }; 4 | let handler = { 5 | default: "NO PROP", 6 | get: function (target, prop, recivier) { 7 | if (prop in target) { 8 | return target[prop]; 9 | } else { 10 | return this.default; 11 | } 12 | }, 13 | 14 | set: function (target, prop, reciver) { 15 | if (prop in target) { 16 | return target[prop]; 17 | } else { 18 | return this.default; 19 | } 20 | }, 21 | }; 22 | 23 | let test = new Proxy(student, handler); 24 | console.log(test.name); 25 | console.log(test.age); 26 | console.log(test.grjklgr); 27 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/2.js: -------------------------------------------------------------------------------- 1 | // let and const 2 | 3 | /* 4 | JavaScript let 5 | JavaScript let is used to declare variables. Previously, variables were declared using the var keyword. 6 | 7 | To learn more about the difference between let and var, visit JavaScript let vs var. 8 | 9 | The variables declared using let are block-scoped. This means they are only accessible within a particular block. For example, 10 | 11 | // variable declared using let 12 | let name = 'Sara'; 13 | { 14 | // can be accessed only inside 15 | let name = 'Peter'; 16 | 17 | console.log(name); // Peter 18 | } 19 | console.log(name); // Sara 20 | JavaScript const 21 | The const statement is used to declare constants in JavaScript. For example, 22 | 23 | // name declared with const cannot be changed 24 | const name = 'Sara'; 25 | Once declared, you cannot change the value of a const variable. 26 | 27 | */ 28 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/3.js: -------------------------------------------------------------------------------- 1 | // Arrow Functions 2 | 3 | // Normal Functions 4 | 5 | // function sayName(name) { 6 | // return name; 7 | // } 8 | 9 | // let x = sayName("Hello"); 10 | // console.log(x); 11 | 12 | // 1. Arrow Function with No Arguments 13 | 14 | let fun = () => console.log("Hello"); 15 | fun(); 16 | 17 | // 2. Arrow Function with 1 Argument 18 | 19 | let fun1 = (name) => console.log("Hello"); 20 | fun1(); 21 | 22 | // 3. Arrow Function with Expression 23 | 24 | let age = 20; 25 | let data = age > 18 ? () => console.log("Adult") : () => console.log("Baby"); 26 | data(); 27 | 28 | // 4. Arrow Function With Multi Line 29 | 30 | let fun2 = (name) => { 31 | console.log(name); 32 | }; 33 | fun2("Hello"); 34 | 35 | // 5. Arguments Binding 36 | 37 | let fun3 = (...n) => { 38 | console.log(n); 39 | }; 40 | 41 | fun3(4, 6, 7, 3, 3, 3); 42 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/4.js: -------------------------------------------------------------------------------- 1 | // Default Parameters 2 | 3 | // function sum(x = 3, y = 5) { 4 | // return x + y; 5 | // } 6 | // let output = sum(5); // x = 5 , y = 5 (by default) = 10 7 | // let output1 = sum(5, 1); // x = 5 , y = 1 = 6 8 | // let output2 = sum(); // x = 3 (by default) and y = 5 (by default) = 8 9 | // console.log(output); 10 | // console.log(output1); 11 | // console.log(output2); 12 | 13 | // Same in Arrow Function 14 | // let sum = (x = 3, y = 5) => x + y; 15 | // let output = sum(5); // x = 5 , y = 5 (by default) = 10 16 | // let output1 = sum(5, 1); // x = 5 , y = 1 = 6 17 | // let output2 = sum(); // x = 3 (by default) and y = 5 (by default) = 8 18 | // console.log(output); 19 | // console.log(output1); 20 | // console.log(output2); 21 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/5.js: -------------------------------------------------------------------------------- 1 | // Template Literals 2 | 3 | const name = "Suhail"; 4 | console.log(`Hello ${name}`); 5 | 6 | // Using Template Literals You can Allow Variables and Embedded Expressions to use in Strings 7 | 8 | let cost = 100; 9 | console.log(`The Price of Product is ${cost * 10}`); 10 | 11 | // Multi Line Using + Operator 12 | 13 | const msg = 14 | "This is a Long Message\n" + 15 | "Its In Next Line As You Can See It\n" + 16 | "Hello\n" + 17 | "No.4\n" + 18 | "No.5"; 19 | console.log(msg); 20 | 21 | // Or Else you can use `` 22 | const msg1 = `Hello I am Long Message 23 | Using Template Literals 24 | No.2 25 | No.3 26 | No.4 27 | No.5 28 | No.6`; 29 | console.log(msg1); 30 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/7.js: -------------------------------------------------------------------------------- 1 | // 7.js 2 | 3 | let contact = (name, age) => console.log(`Name is ${name} and Age is ${age}`); 4 | export default contact; 5 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/8.js: -------------------------------------------------------------------------------- 1 | import contact from "./7.js"; 2 | 3 | contact("Suhail", 20); 4 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/9.js: -------------------------------------------------------------------------------- 1 | // Spread Operator 2 | 3 | let data1 = ["Hello", "I", "am", "Suhail"]; 4 | console.log(data1); 5 | 6 | let data2 = data1; 7 | console.log(data2); 8 | 9 | data1.push("Roushan"); 10 | 11 | console.log(data1); 12 | 13 | console.log(data2); 14 | 15 | let data3 = [...data1]; 16 | console.log(data1); 17 | console.log(data3); 18 | 19 | data1.push("Ali"); 20 | 21 | console.log(data1); 22 | console.log(data3); 23 | 24 | let fun = [1, 2, 3]; 25 | let fun2 = [...fun, 4, 5, 6]; 26 | 27 | console.log(fun2); 28 | 29 | //Objects 30 | 31 | let obj1 = { x: 1, y: 2 }; 32 | let obj2 = { z: 3 }; 33 | 34 | let obj3 = { ...obj1, ...obj2 }; 35 | console.log(obj3); 36 | 37 | // Rest Operator 38 | 39 | let func = function (...args) { 40 | console.log(args); 41 | }; 42 | 43 | func(3); 44 | 45 | func(1, 2, 344); 46 | 47 | function sum(x, y) { 48 | console.log(x + y); 49 | } 50 | 51 | const num = [1, 2, 3, 4]; 52 | 53 | sum(...num); 54 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "14.-es6", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "14.-es6", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "readline-sync": "^1.4.10" 13 | } 14 | }, 15 | "node_modules/readline-sync": { 16 | "version": "1.4.10", 17 | "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", 18 | "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", 19 | "engines": { 20 | "node": ">= 0.8.0" 21 | } 22 | } 23 | }, 24 | "dependencies": { 25 | "readline-sync": { 26 | "version": "1.4.10", 27 | "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", 28 | "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Advanced-JavaScript/14. ES6/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "14.-es6", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "1.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "readline-sync": "^1.4.10" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/1.js: -------------------------------------------------------------------------------- 1 | // setTimeOut 2 | 3 | console.log("Hello"); 4 | 5 | function name() { 6 | console.log("I am Function"); 7 | } 8 | 9 | setTimeout(name, 5000); 10 | console.log("Bye"); 11 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/10.js: -------------------------------------------------------------------------------- 1 | let promise = new Promise((resolve, reject) => { 2 | resolve("Good"); 3 | }); 4 | 5 | let data = async () => { 6 | try { 7 | console.log("Hello"); 8 | } catch (error) { 9 | console.log(error); 10 | } 11 | }; 12 | 13 | data() 14 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/11.js: -------------------------------------------------------------------------------- 1 | let data = async () => { 2 | let p1 = new Promise((resolve, reject) => { 3 | setTimeout(() => { 4 | resolve("p1 took 5 Seconds"); 5 | }, 5000); 6 | }); 7 | 8 | let p2 = new Promise((resolve, reject) => { 9 | setTimeout(() => { 10 | resolve("p2 took 3 seconds"); 11 | }, 3000); 12 | }); 13 | let s1 = await p1; 14 | let s2 = await p2; 15 | console.log(s1); 16 | console.log(s2); 17 | }; 18 | 19 | data(); 20 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/12.js: -------------------------------------------------------------------------------- 1 | // setInterval() and ClearInterval() 2 | 3 | let count = 0; 4 | 5 | let data = setInterval(function () { 6 | console.log("Hello "); 7 | 8 | count = count + 1; 9 | if (count == 2) { 10 | clearInterval(data); 11 | } 12 | }, 1000); 13 | 14 | console.log(data); 15 | 16 | 17 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/2.js: -------------------------------------------------------------------------------- 1 | console.log("Hello"); 2 | 3 | let store = setTimeout(() => { 4 | console.log("I am SetTimeOut"); 5 | },5000); 6 | clearTimeout(store); 7 | 8 | console.log(`Id : ${store}`); 9 | console.log("End"); 10 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/3.js: -------------------------------------------------------------------------------- 1 | console.log("Started"); 2 | 3 | let data = setTimeout(() => { 4 | for (let i = 0; i <= 10; i++) { 5 | console.log(i); 6 | } 7 | }, 10000); 8 | 9 | clearTimeout(data); 10 | console.log("Ended"); 11 | for (let i = 0; i <= 10; i++) { 12 | console.log(i * 2); 13 | } 14 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/5.js: -------------------------------------------------------------------------------- 1 | // CallBack Function 2 | 3 | console.log("Hello"); 4 | 5 | let fun1 = (name, fun) => { 6 | console.log(`Hello ${name}`); 7 | fun(); 8 | }; 9 | 10 | let fun2 = (name) => { 11 | console.log("I am Fun2"); 12 | }; 13 | 14 | fun1("Suhail", fun2); 15 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/6.js: -------------------------------------------------------------------------------- 1 | function greet() { 2 | console.log("Hello I am Greet"); 3 | } 4 | 5 | function sayName(name) { 6 | console.log(name); 7 | } 8 | 9 | setTimeout(greet, 5000); 10 | sayName("Suhail"); 11 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/7.js: -------------------------------------------------------------------------------- 1 | function greet(name, myfun) { 2 | console.log("Hello Main Greet",name); 3 | myfun(name); 4 | } 5 | 6 | function sayName(name) { 7 | console.log("Hello ", name); 8 | } 9 | 10 | setTimeout(greet, 4000, "Suhail", sayName); 11 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/8.js: -------------------------------------------------------------------------------- 1 | // Promises 2 | 3 | let data = new Promise(function (resolve, reject) { 4 | resolve(); 5 | reject(); 6 | }); 7 | 8 | data 9 | .then(function () { 10 | console.log("I am Cool"); 11 | }) 12 | .then(() => { 13 | console.log("I am From Coool 2", de); 14 | }) 15 | .then(() => { 16 | console.log("I am tHIRD Cool"); 17 | }) 18 | .catch(() => { 19 | console.log("I am Error"); 20 | }); 21 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/9.js: -------------------------------------------------------------------------------- 1 | import fetch from "node-fetch"; 2 | let data = "https://api.sgithub.com/users/suhailroushan13"; 3 | 4 | let user = fetch(data); 5 | 6 | console.log(user); 7 | 8 | user 9 | .then((d) => { 10 | console.log(d, d.urls); 11 | console.log(d.statuss); 12 | }) 13 | .catch(() => { 14 | console.log("Error hai yeh"); 15 | }); 16 | // A Promises is an Place Holder for a certain period of time ...which will be filled with value later from asynchronous operations 17 | 18 | // A container for future value == Promises 19 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 |

JavaScript Promises

11 | 12 | 13 | -------------------------------------------------------------------------------- /Advanced-JavaScript/15. JavaScript Asynchronous/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "15.-javascript-asynchronous", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "1.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "node-fetch": "^2.6.7" 15 | } 16 | } -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/1.js: -------------------------------------------------------------------------------- 1 | // JSON JavaScript Object Notation 2 | 3 | /* 4 | 1. In JSON Both key/value should be in double quotation 5 | 2. You cannot use functions in JSON 6 | 3. JSON can be use in any programming languages 7 | 4. JavaScript Objects can be use in only in JS 8 | 9 | 10 | */ 11 | 12 | 13 | // Example of JSON 14 | 15 | // let data = { 16 | // "name": "Suhail", 17 | // "age": 20, 18 | // "isAlive":true 19 | // } 20 | 21 | // Both key/value should be in double quotation 22 | 23 | 24 | -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/1.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Suhail", 3 | "age": 20, 4 | "isAlive": true, 5 | "address": { 6 | "city": "Hyderabad", 7 | "zip": 500045 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/11.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suhailroushan13/You-Should-Know-JavaScript/d475a244c82c1ccb4eed3f33e798b63de59e5eca/Advanced-JavaScript/16. JavaScript Miscellaneous/11.js -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/2.js: -------------------------------------------------------------------------------- 1 | // JSON With Array 2 | 3 | let data = { 4 | "name": "Suhail", 5 | "age": 20, 6 | "isAlive": true, 7 | "address": { 8 | "city": "Hyderabad", 9 | "zip": 500045 10 | } 11 | } 12 | 13 | console.log(data.address); 14 | console.log(data["address"]); 15 | console.log(data["address"]["city"]); 16 | console.log(data.address.city); 17 | 18 | 19 | let obj = JSON.stringify(data) 20 | console.log(obj); 21 | -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/2.jsonc: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Suhail", 3 | "age": 20, 4 | "isAlive": true, 5 | "address": { 6 | "city": "Hyderabad", 7 | "zip": 500045 8 | } 9 | } 10 | // This is a File name JSONC 11 | // JavaScript Object Notation With Comments 12 | /* 13 | Use of JSON 14 | JSON is the most commonly used format for 15 | transmitting data (data interchange) from a 16 | server to a client and vice-versa. 17 | JSON data are very easy to parse and use. 18 | It is fast to access and manipulate JSON 19 | data as they only contain texts. 20 | 21 | JSON is language independent. 22 | You can create and use JSON in other programming languages too. 23 | 24 | 25 | */ -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/3.js: -------------------------------------------------------------------------------- 1 | let data = 2 | '{ "name": "Suhail", "age": 20, "isAlive": true, "address": { "city": "Hyderabad", "zip": 500045 } }'; 3 | 4 | let json = JSON.parse(data); 5 | console.log(json); 6 | console.log(json.name); 7 | console.log(json.address.city); 8 | -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/4.js: -------------------------------------------------------------------------------- 1 | // Date Object 2 | /* 3 | 4 Ways to create date object 4 | 5 | 1. new Date() 6 | 2. new Date(milliseconds) 7 | 3. new Date(date string) 8 | 4. new Date(year,months,day,hours,minutes,seconds,milliseconds) 9 | 10 | 11 | 12 | 13 | */ 14 | 15 | let a1 = new Date(); 16 | console.log(a1); // 2022-11-30T04:35:41.520Z 17 | 18 | let a2 = new Date(2000000000000); 19 | console.log(a2); // 2033-05-18T03:33:20.000Z 20 | 21 | let a3 = new Date("30 Nov 2022"); 22 | console.log(a3); // 2022-11-30T00:00:00.000Z 23 | 24 | let a4 = new Date(2002, 01, 11, 10, 0, 0, 0); 25 | console.log(a4); // 2002-02-11T10:00:00.000Z 26 | 27 | 28 | let a5 = Date.now(); 29 | console.log(a5); // 1669783234656 30 | 31 | let a6 = new Date(1669783234656); 32 | console.log(a6); // 2022-11-30T04:40:34.656Z 33 | 34 | 35 | 36 | 37 | 38 | /* 39 | JavaScript Date Methods 40 | There are various methods available in JavaScript Date object. 41 | 42 | Method Description 43 | now() Returns the numeric value corresponding to the current time (the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC) 44 | getFullYear() Gets the year according to local time 45 | getMonth() Gets the month, from 0 to 11 according to local time 46 | getDate() Gets the day of the month (1–31) according to local time 47 | getDay() Gets the day of the week (0-6) according to local time 48 | getHours() Gets the hour from 0 to 23 according to local time 49 | getMinutes Gets the minute from 0 to 59 according to local time 50 | getUTCDate() Gets the day of the month (1–31) according to universal time 51 | setFullYear() Sets the full year according to local time 52 | setMonth() Sets the month according to local time 53 | setDate() Sets the day of the month according to local time 54 | setUTCDate() Sets the day of the month according to universal time 55 | 56 | 57 | 58 | 59 | 60 | 61 | */ -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/5.js: -------------------------------------------------------------------------------- 1 | // Clousers 2 | 3 | function sum(name) { 4 | function newSum() { 5 | return "Hi" + name; 6 | } 7 | return newSum; 8 | } 9 | 10 | let a = sum(" Roushan"); 11 | console.log(a); 12 | console.log(a()); 13 | 14 | // Clousers are basically used for Data Privacy 15 | 16 | let value = 0; 17 | function cool() { 18 | function newCool() { 19 | return (value += 1); 20 | } 21 | return newCool; 22 | } 23 | 24 | let b = cool(); 25 | console.log(b); 26 | console.log(b()); 27 | console.log(b()); 28 | console.log(b()); 29 | console.log(b()); 30 | 31 | // console.log(c()); 32 | 33 | 34 | -------------------------------------------------------------------------------- /Advanced-JavaScript/16. JavaScript Miscellaneous/6.js: -------------------------------------------------------------------------------- 1 | // In JavaScript, this keyword refers to the object where it is called. 2 | // 1. this Inside Global Scope 3 | // When this is used alone, this refers to the global object (window object in browsers). For example, 4 | 5 | // let a = this; 6 | // console.log(a); // Window {} 7 | 8 | // this.name = "Sarah"; 9 | // console.log(window.name); // Sarah // Works in Only Browser 10 | 11 | // 2. this Inside a Function 12 | 13 | function greet() { 14 | // this inside function 15 | // this refers to the global object 16 | // console.log(this); 17 | } 18 | 19 | // greet(); // Window {} 20 | 21 | class Person { 22 | constructor() { 23 | this.name = "Suhail"; 24 | // console.log(this); 25 | } 26 | } 27 | 28 | // let person = new Person(); 29 | // console.log(person); 30 | 31 | // this Inside an Object 32 | 33 | const obj = { 34 | name: "Suhail", 35 | age: 20, 36 | isAlive: true, 37 | address: { 38 | zip: 500045, 39 | area: "Hyderabad", 40 | }, 41 | fun() { 42 | return `${this.name} ${this.age}`; 43 | }, 44 | }; 45 | 46 | let v1 = obj.fun(); 47 | // console.log(v1); 48 | 49 | // Arrow functions do not have their own this. When you use this inside an arrow function, this refers to its parent scope object. For example, 50 | 51 | let person = { 52 | name: "Suhail", 53 | age: 20, 54 | isAlive: true, 55 | bankBalance() { 56 | let s1 = () => { 57 | return console.log(`My Name is ${this.name} and My Age is ${this.age}`); 58 | }; 59 | s1(); 60 | }, 61 | }; 62 | 63 | person.bankBalance() -------------------------------------------------------------------------------- /Pratice/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .vscode 3 | .cache 4 | .history 5 | -------------------------------------------------------------------------------- /Pratice/1.js: -------------------------------------------------------------------------------- 1 | let ans1 = new Date().toLocaleString("ens-US",{ 2 | weekday:"long", 3 | day:"2-digit", 4 | hour:"2-digit", 5 | minute:"2-digit", 6 | second:"2-digit" 7 | }) 8 | 9 | console.log(ans1); 10 | -------------------------------------------------------------------------------- /Pratice/10.js: -------------------------------------------------------------------------------- 1 | class Tariq { 2 | constructor(date, month, year) { 3 | this.date = new Date().getDate(); 4 | this.month = new Date().getMonth() + 1; 5 | this.year = new Date().getFullYear(); 6 | } 7 | } 8 | 9 | const tariq = new Tariq(); 10 | console.log(tariq); 11 | 12 | console.log(tariq.date, tariq.month); 13 | console.log(tariq.month); 14 | console.log(tariq.year); 15 | -------------------------------------------------------------------------------- /Pratice/11.js: -------------------------------------------------------------------------------- 1 | let i = 0 2 | setInterval(() => { 3 | for (; i <= 10; i++) { 4 | console.log(i); 5 | i++; 6 | break; 7 | 8 | } 9 | }, 1000) 10 | 11 | -------------------------------------------------------------------------------- /Pratice/12.js: -------------------------------------------------------------------------------- 1 | const suhail = [10, 20, 30] 2 | console.log(suhail); // 10 20 30 3 | 4 | const ayeen = suhail; 5 | console.log(ayeen); // 10 20 30 6 | 7 | suhail[0] = 50; 8 | suhail[7] = 45; 9 | console.log(suhail); 10 | console.log(ayeen); 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Pratice/13.js: -------------------------------------------------------------------------------- 1 | // defining an array named alphabets 2 | const alphabets = ["A", "B", "C"]; 3 | 4 | // array iterator object that contains 5 | // key-value pairs for each index in the array 6 | let iterator = alphabets.entries(); 7 | 8 | console.log(iterator); 9 | 10 | for (let entry of iterator) { 11 | console.log(entry); 12 | } 13 | -------------------------------------------------------------------------------- /Pratice/14.js: -------------------------------------------------------------------------------- 1 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 2 | 3 | console.log(arr.at(3)); 4 | 5 | console.log(arr.at(5)); 6 | -------------------------------------------------------------------------------- /Pratice/15.js: -------------------------------------------------------------------------------- 1 | const words = ["zero", "one", "two", "three"]; 2 | 3 | console.log(words); 4 | console.log(words.copyWithin(1, 2, 3)); 5 | console.log(words); 6 | -------------------------------------------------------------------------------- /Pratice/19.js: -------------------------------------------------------------------------------- 1 | // let a1 = { 2 | // name: "Suhail", 3 | // age: 20, 4 | // }; 5 | // let a2 = Object.assign(a1); 6 | // console.log(a2); 7 | // a2.isAlive = true; 8 | 9 | // console.log(a2); 10 | // console.log(a1); 11 | 12 | // let student = { 13 | // name: "suhail", 14 | // age: 20, 15 | // }; 16 | 17 | // // delete student.age 18 | // console.log(student); 19 | 20 | // let obj = Object.create({ 21 | // name: "suhail", 22 | // age: 20, 23 | // }); 24 | 25 | // console.log(obj); // empty 26 | // console.log(obj.name,obj.age); // suhail 27 | 28 | // obj.isAlive = true; 29 | // console.log(obj); 30 | // console.log(obj.name,obj.age); 31 | 32 | // let a1 = { 33 | // name: "Suhail", 34 | // age: 22, 35 | // address: { 36 | // city: "Hyd", 37 | // }, 38 | // }; 39 | 40 | // Object.freeze(a1); 41 | 42 | // console.log(a1); 43 | // a1.isAlive = true // Add 44 | // a1.name = "Roushan" // Update 45 | // a1.address.city= "mumbai" 46 | // delete a1.address // delete 47 | 48 | // console.log(a1); 49 | 50 | // console.log(Object.isFrozen(a1)); 51 | 52 | // let a1 = { 53 | // name: "Suhail", 54 | // age: 22, 55 | // address: { 56 | // city: "Hyd", 57 | // }, 58 | // }; 59 | 60 | // Object.seal(a1); 61 | // console.log(Object.isSealed(a1)); 62 | 63 | // console.log(a1); 64 | 65 | // // a1.address.city = true; 66 | // a1.name = "Roushan" 67 | // // 68 | // // a1.isAlive = true; 69 | 70 | // delete a1.address 71 | 72 | // console.log(a1); 73 | 74 | let a1 = { 75 | name: "Suhail", 76 | age: 22, 77 | address: { 78 | city: "Hyd", 79 | }, 80 | }; 81 | 82 | console.log(a1); 83 | 84 | 85 | Object.preventExtensions(a1) 86 | console.log(a1); 87 | console.log(Object.isExtensible(a1)); 88 | 89 | 90 | console.log(a1); 91 | 92 | a1.name = "Roushan" 93 | a1.address.city = "Mumbai" 94 | a1.address.area = "local" 95 | // delete a1.address 96 | 97 | 98 | console.log(a1); 99 | -------------------------------------------------------------------------------- /Pratice/2.js: -------------------------------------------------------------------------------- 1 | let ans1 = new Date().toLocaleString() 2 | console.log(ans1); 3 | -------------------------------------------------------------------------------- /Pratice/20.js: -------------------------------------------------------------------------------- 1 | const a1 = { 2 | name: "Suhail", 3 | age: 20, 4 | }; 5 | 6 | const { name: fullname, age: saal } = { ...a1 }; 7 | 8 | const a2 = Object.assign( 9 | {}, 10 | { 11 | fullname, 12 | saal, 13 | } 14 | ); 15 | 16 | console.log(a2); 17 | -------------------------------------------------------------------------------- /Pratice/21.js: -------------------------------------------------------------------------------- 1 | let a = 1; 2 | let b = 2; 3 | let c = "1"; 4 | let name = "Hello"; 5 | let name1 = "Hello"; 6 | let name2 = "hello"; 7 | 8 | console.log(a == b); // false 9 | console.log(a == c); // true 10 | console.log(name == name1); // true 11 | console.log(name == name2); // false 12 | 13 | console.log(name === name1); 14 | 15 | -------------------------------------------------------------------------------- /Pratice/22.js: -------------------------------------------------------------------------------- 1 | // Symbol 2 | 3 | let a = "Hello"; 4 | let b = "Hello"; 5 | 6 | console.log(a == b); // true 7 | console.log(a === b); // true 8 | 9 | let symbolA = Symbol("Hello"); 10 | let symbolB = Symbol("Hello"); 11 | console.log(symbolA == symbolB); // false 12 | console.log(symbolA === symbolB); // false 13 | console.log(typeof symbolA, typeof symbolB); // symbol symbol 14 | 15 | let person = { 16 | name: "Suhail", 17 | age: 20, 18 | isAlive: true, 19 | number: 9618211626, 20 | }; 21 | console.log(person); 22 | person.school = "Nalanda"; 23 | 24 | console.log(person); 25 | 26 | for (let i in person) { 27 | console.log(i, person[i]); 28 | } 29 | 30 | let college = Symbol("college"); 31 | college = Symbol("collegesss"); 32 | 33 | person[college] = "Chaitanya"; 34 | person[college] = "Chaitanyasss"; 35 | 36 | console.log(person); 37 | -------------------------------------------------------------------------------- /Pratice/23.js: -------------------------------------------------------------------------------- 1 | // Symbolfor 2 | 3 | let sym = Symbol.for("Hello"); 4 | let sym1 = Symbol.for("Hello1"); 5 | 6 | console.log(Symbol.keyFor(sym)); // Hello 7 | console.log(Symbol.keyFor(sym1)); // Hello1 8 | 9 | let a = Symbol("Hello I am Suhail"); 10 | console.log(a.description); 11 | ĺ -------------------------------------------------------------------------------- /Pratice/24.js: -------------------------------------------------------------------------------- 1 | // Symbol Properties 2 | // Properties Description 3 | // asyncIterator Returns the default AsyncIterator for an object 4 | // hasInstance Determines if a constructor object recognizes an object as its instance 5 | // isConcatSpreadable Indicates if an object should be flattened to its array elements 6 | // iterator Returns the default iterator for an object 7 | // match Matches against a string 8 | // matchAll Returns an iterator that yields matches of the regular expression against a string 9 | // replace Replaces matched substrings of a string 10 | // search Returns the index within a string that matches the regular expression 11 | // split Splits a string at the indices that match a regular expression 12 | // species Creates derived objects 13 | // toPrimitive Converts an object to a primitive value 14 | // toStringTag Gives the default description of an object 15 | // description Returns a string containing the description of the symbol 16 | 17 | 18 | const x = Symbol('hey'); 19 | 20 | // description property 21 | console.log(x.description); // hey 22 | 23 | const stringArray = ['a', 'b', 'c']; 24 | const numberArray = [1, 2, 3]; 25 | 26 | // isConcatSpreadable property 27 | numberArray[Symbol.isConcatSpreadable] = false; 28 | 29 | let result = stringArray.concat(numberArray); 30 | console.log(result); // ["a", "b", "c", [1, 2, 3]] -------------------------------------------------------------------------------- /Pratice/25.js: -------------------------------------------------------------------------------- 1 | import readline from "readline-sync"; 2 | 3 | let ask = readline.questionInt("Enter Number : "); 4 | 5 | if (ask % 15 == 0) { 6 | console.log(`${ask} is divisble by 3 and 5 and FIZZBUSS`); 7 | } else { 8 | if (ask % 3 == 0) { 9 | console.log(`${ask} is divisble by 3 and FIZZ`); 10 | } 11 | if (ask % 5 == 0) { 12 | console.log(`${ask} is divisble by 5 and BUSS`); 13 | } 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /Pratice/26.js: -------------------------------------------------------------------------------- 1 | import readline from "readline-sync"; 2 | 3 | let a = readline.questionInt("Enter a :"); 4 | let b = readline.questionInt("Enter b :"); 5 | let c = readline.questionInt("Enter b :"); 6 | 7 | let num = -b + Math.sqrt(b * b - 4 * a * c); 8 | let num1 = -b - Math.sqrt(b * b - 4 * a * c); 9 | 10 | let deno = 2 * a; 11 | console.log(num / deno); 12 | console.log(num1 / deno); 13 | -------------------------------------------------------------------------------- /Pratice/27.js: -------------------------------------------------------------------------------- 1 | let a = new Map(); 2 | console.log(a); 3 | 4 | a.set("person", { name: "Suhail" }); 5 | 6 | console.log(a); 7 | 8 | console.log(a.get("person")); 9 | 10 | console.log(a.has("person")); 11 | 12 | a.delete("person"); 13 | console.log(a); 14 | 15 | a.set("1", "Suhail"); 16 | a.set("2", "Roushan"); 17 | 18 | console.log(a); 19 | 20 | for (let key of a) { 21 | console.log(key); 22 | } 23 | 24 | // WeakMap can only have Object as Keys 25 | // you cannot iterate over WeakMap 26 | let b = new WeakMap(); 27 | console.log(b); 28 | let obj = {}; 29 | b.set(obj, "Hello"); 30 | console.log(b); 31 | console.log(b.get(obj)); 32 | console.log(b.has(obj)); 33 | console.log(b.delete(obj)); 34 | console.log(b); 35 | 36 | console.log(b.get(obj)); 37 | -------------------------------------------------------------------------------- /Pratice/28.js: -------------------------------------------------------------------------------- 1 | // Set 2 | 3 | let a = new Set([1, 2, 3, 4, 5, 1]); 4 | let b = new Set([1, 2, 3, 1, 1, 1, 1]); 5 | console.log(a); 6 | console.log(b); 7 | 8 | a.add(10); 9 | console.log(a.values()); 10 | console.log(a.has(2)); 11 | console.log(a.delete(10)); 12 | console.log(a); 13 | console.log(a.clear()); 14 | console.log(a); 15 | -------------------------------------------------------------------------------- /Pratice/29.js: -------------------------------------------------------------------------------- 1 | // Classes 2 | 3 | // function Person() { 4 | // this.name = "Suhail"; 5 | // this.age = 24; 6 | // } 7 | 8 | // let person = new Person(); 9 | // console.log(person); 10 | 11 | // A class may only have one constructor 12 | class Person { 13 | constructor(name, age) { 14 | this.name = name; 15 | this.age = age; 16 | } 17 | } 18 | 19 | let person = new Person("Roushan", 23); 20 | console.log(person); 21 | 22 | let person2 = new Person("Ali", 20); 23 | console.log(person2.name, person2.age); 24 | 25 | 26 | -------------------------------------------------------------------------------- /Pratice/3.js: -------------------------------------------------------------------------------- 1 | let ans; 2 | 3 | function area(b,h){ 4 | return (b*h)/2 5 | } 6 | 7 | console.log(area(10, 20)); 8 | 9 | -------------------------------------------------------------------------------- /Pratice/30.js: -------------------------------------------------------------------------------- 1 | class Student { 2 | constructor(name, age) { 3 | this.name = name; 4 | this.age = age; 5 | } 6 | 7 | greet() { 8 | return console.log(`${this.name} and ${this.age}`); 9 | } 10 | } 11 | 12 | let person = new Student("Suhail", 20); 13 | console.log(person); 14 | person.greet(); 15 | -------------------------------------------------------------------------------- /Pratice/31.js: -------------------------------------------------------------------------------- 1 | class Student { 2 | constructor(name) { 3 | this.name = name; 4 | } 5 | get gettingName() { 6 | return name; 7 | } 8 | set settingName(x) { 9 | this.name = x; 10 | } 11 | } 12 | 13 | let a1 = new Student("Hello"); 14 | a1.settingName = "Suhail"; 15 | console.log(a1); 16 | 17 | class Person {} 18 | 19 | console.log(typeof Person); 20 | -------------------------------------------------------------------------------- /Pratice/4.js: -------------------------------------------------------------------------------- 1 | let ans = function (text) { 2 | let sum; 3 | setInterval(() => { 4 | for (let i = 0; i <= text.length; i++) { 5 | sum = text[i]; 6 | let arr = [] 7 | arr.push(sum) 8 | console.log(arr); 9 | } 10 | }, 1000); 11 | }; 12 | ans("hello"); 13 | -------------------------------------------------------------------------------- /Pratice/5.js: -------------------------------------------------------------------------------- 1 | let ans=1900; 2 | ans = (ans%4==0 && ans % 100 ==0)?"Leap":"Not Leap" 3 | console.log(ans); 4 | -------------------------------------------------------------------------------- /Pratice/6.js: -------------------------------------------------------------------------------- 1 | function calculate_age(dob) { 2 | var diff_ms = Date.now() - dob.getTime(); 3 | var age_dt = new Date(diff_ms); 4 | 5 | return Math.abs(age_dt.getUTCFullYear() - 1970); 6 | } 7 | 8 | console.log(calculate_age(new Date(1982, 11, 4))); 9 | 10 | console.log(calculate_age(new Date(2002, 02, 11))); 11 | -------------------------------------------------------------------------------- /Pratice/7.js: -------------------------------------------------------------------------------- 1 | let sum = 1; 2 | function fac(num) { 3 | for (let i = num; i >= 1; i--) { 4 | sum *= i; 5 | } 6 | console.log(sum); 7 | } 8 | fac(1); 9 | 10 | function fac(num) { 11 | return num == 1 || num == 0 ? 1 : num * fac(num - 1); 12 | } 13 | let n = 10; 14 | console.log(fac(n)); 15 | -------------------------------------------------------------------------------- /Pratice/8.js: -------------------------------------------------------------------------------- 1 | // While Creating a Object 2 | // const obj = { 3 | // name: "Helo", 4 | // age: 23, 5 | // fun: function name() { 6 | // return "Hello"; 7 | 8 | // }, 9 | // isAlive: true 10 | // } 11 | // console.log(typeof obj, obj); 12 | // console.log(obj.fun()); 13 | 14 | // 15 | 16 | 17 | // const obj2 = { 18 | // name: "Hello", 19 | // age: 20, 20 | // fun: fac(), 21 | // isAlive: true, 22 | // } 23 | 24 | // function fac() { 25 | // return "Hello" 26 | // } 27 | 28 | // console.log(typeof obj2, obj2); 29 | // console.log(obj2.fun); 30 | 31 | // const obj3 = { 32 | // name: 'Suhail', 33 | // age: 20, 34 | // fun() { 35 | // return "Hello" 36 | // } 37 | // } 38 | // console.log(typeof obj3, obj3.fun(),obj3); 39 | let a = this; 40 | console.log(a); 41 | this.name = 'Sarah'; 42 | console.log(window.name); // Sarah -------------------------------------------------------------------------------- /Pratice/9.js: -------------------------------------------------------------------------------- 1 | // let date = new Date().toLocaleString("en-US", 2 | // { 3 | // timeZone: "Asia/Kolkata", 4 | // month: "numeric", 5 | // day: "2-digit", 6 | // year: "numeric", 7 | // weekday: "long", 8 | // hour: "2-digit", 9 | // minute: "numeric", 10 | // second: "2-digit", 11 | // dayPeriod: "long", 12 | // }) 13 | // console.log(date); 14 | let a = new Date(1664575997403).toDateString("en-US",{timeZone:"Aisa/Kolkata"}) 15 | console.log(a); 16 | 17 | -------------------------------------------------------------------------------- /Pratice/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pratice", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "pratice", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "readline-sync": "^1.4.10" 13 | } 14 | }, 15 | "node_modules/readline-sync": { 16 | "version": "1.4.10", 17 | "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", 18 | "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", 19 | "engines": { 20 | "node": ">= 0.8.0" 21 | } 22 | } 23 | }, 24 | "dependencies": { 25 | "readline-sync": { 26 | "version": "1.4.10", 27 | "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", 28 | "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Pratice/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pratice", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "1.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "readline-sync": "^1.4.10" 15 | } 16 | } --------------------------------------------------------------------------------