├── 1-Getting Started With JavaScript.md ├── 10. switch statement.md ├── 11. while Loop.md ├── 12. for loop.md ├── 13. break and continue.md ├── 14. JavaScript Functions.md ├── 15. Arrow functions.md ├── 16. Global and local scope.md ├── 17. JavaScript Objects.md ├── 18. Strings.md ├── 19. 10 String Methods_Properties You Must know.md ├── 2. JavaScript Variables.md ├── 20. JavaScript Array.md ├── 21. Array Methods You Must Know.md ├── 22. map, filter, reduce.md ├── 23. for...in and for...of Loop.md ├── 24. Numbers.md ├── 3. JavaScript Take Input.md ├── 4. JavaScript Comments.md ├── 5. JavaScript Operators and Arithmetic operators.md ├── 6. Booleans and Boolean Expression.md ├── 7. Basic Type conversion.md ├── 8. if_else Statement.md ├── 9. Ternary Operator.md └── readme /1-Getting Started With JavaScript.md: -------------------------------------------------------------------------------- 1 | # 1- Getting Started With JavaScript 2 | *** 3 | JavaScript is one of the most popular and dynamic programming languages out there. 4 | ## Programs in the Videos 5 | ```js 6 | console.log("Hello, World"); 7 | ``` 8 | **Output** 9 | 10 | ``` 11 | Hello, World 12 | ``` 13 | The console.log() prints whatever object that is inside the parentheses `()`. 14 | 15 | -------------------------------------------------------------------------------- /10. switch statement.md: -------------------------------------------------------------------------------- 1 | # 10. switch Statement 2 | ## Example 3 | ```js 4 | const number = parseInt(prompt('Enter a number between 1 to 7: ')); 5 | 6 | switch (number) { 7 | case 1: 8 | console.log("Sunday"); 9 | break; 10 | case 2: 11 | console.log("Monday"); 12 | break; 13 | case 3: 14 | console.log("Tuesday"); 15 | break; 16 | case 4: 17 | console.log("Wednesday"); 18 | break; 19 | case 5: 20 | console.log("Thursday"); 21 | break; 22 | case 6: 23 | console.log("Friday"); 24 | break; 25 | case 7: 26 | console.log("Saturday"); 27 | break; 28 | default: 29 | console.log("Invalid Number"); 30 | } 31 | ``` 32 | **Output** 33 | ``` 34 | Enter a number between 1 to 7: 4 35 | Wednesday 36 | ``` 37 | *** 38 | ## Omitting break 39 | ```js 40 | const number = parseInt(prompt('Enter a number between 1 to 7: ')); 41 | 42 | switch (number) { 43 | case 1: 44 | console.log("Sunday"); 45 | case 2: 46 | console.log("Monday"); 47 | case 3: 48 | console.log("Tuesday"); 49 | case 4: 50 | console.log("Wednesday"); 51 | case 5: 52 | console.log("Thursday"); 53 | case 6: 54 | console.log("Friday"); 55 | case 7: 56 | console.log("Saturday"); 57 | default: 58 | console.log("Invalid Number"); 59 | } 60 | ``` 61 | **Output** 62 | ``` 63 | Wednesday 64 | Thursday 65 | Friday 66 | Saturday 67 | Invalid Number 68 | ``` 69 | *** 70 | ## JavaScript switch With Multiple Case 71 | ```js 72 | const day = prompt('Enter the day: '); 73 | 74 | switch (day) { 75 | case 'Monday': 76 | case 'Tuesday': 77 | case 'Wednesday': 78 | case 'Thursday':; 79 | case 'Friday': 80 | console.log("Weekday"); 81 | break; 82 | case 'Saturday': 83 | case 'Sunday': 84 | console.log("Weekend"); 85 | break; 86 | default: 87 | console.log("Invalid Day"); 88 | } 89 | ``` 90 | **Output** 91 | ``` 92 | Weekday 93 | ``` 94 | ## Simple Calculator 95 | ```js 96 | // take the operator input 97 | const operator = prompt('Enter operator ( either +, -, * or / ): '); 98 | 99 | // take the operand input 100 | const number1 = parseFloat(prompt('Enter first number: ')); 101 | const number2 = parseFloat(prompt('Enter second number: ')); 102 | 103 | let result; 104 | switch(operator) { 105 | case '+': 106 | result = number1 + number2; 107 | break; 108 | case '-': 109 | result = number1 - number2; 110 | break; 111 | case '*': 112 | result = number1 * number2; 113 | break; 114 | case '/': 115 | result = number1 / number2; 116 | break; 117 | 118 | default: 119 | console.log('Invalid operator'); 120 | break; 121 | } 122 | console.log(`${number1} ${operator} ${number2} = ${result}`); 123 | ``` 124 | **Output** 125 | ``` 126 | Enter operator: * 127 | Enter first number: 5 128 | Enter second number: 8.8 129 | 5 * 8.8 = 44 130 | ``` 131 | *** 132 | ## Programming Task 133 | **Can you use switch statements to create a program that takes the number input from the user from 1 to 12. And, print the corresponding month based on the input value.** 134 | ### Solution: 135 | ```js 136 | const number = parseInt(prompt('Enter a number from 1 to 12: ')); 137 | 138 | let result; 139 | switch(number) { 140 | case 1: 141 | result = "January"; 142 | break; 143 | case 2: 144 | result = "Febraury"; 145 | break; 146 | case 3: 147 | result = "March"; 148 | break; 149 | case 4: 150 | result = "April"; 151 | break; 152 | case 5: 153 | result = "May"; 154 | break; 155 | case 6: 156 | result = "June"; 157 | break; 158 | case 7: 159 | result = "July"; 160 | break; 161 | case 8: 162 | result = "August"; 163 | break; 164 | case 9: 165 | result = "September"; 166 | break; 167 | case 10: 168 | result = "October"; 169 | break; 170 | case 11: 171 | result = "November"; 172 | break; 173 | case 12: 174 | result = "December"; 175 | break; 176 | default: 177 | console.log('Invalid number'); 178 | break; 179 | } 180 | console.log(result); 181 | ``` 182 | **Output** 183 | ``` 184 | Enter a number from 1 to 12: 3 185 | March 186 | ``` 187 | *** 188 | ## Programiz Quiz 189 | **Q. Which of the cases is executed in the following code?** 190 | ```js 191 | let value = 4; 192 | 193 | switch(value) { 194 | case 1: 195 | case 2: 196 | default: 197 | } 198 | ``` 199 | 1. case 1 200 | 2. case 2 201 | 3. case 4 202 | 4. default 203 | 204 | Answer: 4 205 | -------------------------------------------------------------------------------- /11. while Loop.md: -------------------------------------------------------------------------------- 1 | # 11. while Loop 2 | ## Infinite Loop Example 3 | ```js 4 | while (1 < 5) { 5 | console.log("Learning while loop."); 6 | console.log("JavaScript is fun."); 7 | } 8 | ``` 9 | **Output** 10 | ``` 11 | Learning while loop. 12 | JavaScript is fun. 13 | Learning while loop. 14 | JavaScript is fun. 15 | Learning while loop. 16 | JavaScript is fun. 17 | Learning while loop. 18 | JavaScript is fun. 19 | Learning while loop. 20 | JavaScript is fun. 21 | ............ 22 | ``` 23 | *** 24 | ## while Example 1 25 | ```js 26 | let count = 1; 27 | while (count < 5) { 28 | console.log("Learning while loop."); 29 | console.log("JavaScript is fun."); 30 | count = count + 1; 31 | } 32 | ``` 33 | **Output** 34 | ``` 35 | Learning while loop. 36 | JavaScript is fun. 37 | Learning while loop. 38 | JavaScript is fun. 39 | Learning while loop. 40 | JavaScript is fun. 41 | Learning while loop. 42 | JavaScript is fun. 43 | ``` 44 | *** 45 | ## while loop Example 2 46 | ```js 47 | let count = 1; 48 | while (count < 5) { 49 | console.log("Learning while loop."); 50 | console.log("JavaScript is fun."); 51 | console.log(count); 52 | count = count + 1; 53 | } 54 | ``` 55 | **Output** 56 | ``` 57 | Learning while loop. 58 | JavaScript is fun. 59 | 1 60 | Learning while loop. 61 | JavaScript is fun. 62 | 2 63 | Learning while loop. 64 | JavaScript is fun. 65 | 3 66 | Learning while loop. 67 | JavaScript is fun. 68 | 4 69 | ``` 70 | ## Creating Multiplication Table 71 | ```js 72 | const number = parseInt(prompt("Enter a number: ")); 73 | let count = 1; 74 | while (count <= 10) { 75 | const product = number * count; 76 | console.log(product); 77 | count = count + 1; 78 | } 79 | ``` 80 | **Output** 81 | ``` 82 | Enter a number: 9 83 | 9 84 | 18 85 | 27 86 | 36 87 | 45 88 | 54 89 | 63 90 | 72 91 | 81 92 | 90 93 | ``` 94 | *** 95 | ## Useful Output Message 96 | ```js 97 | const number = parseInt(prompt("Enter a number: ")); 98 | let count = 1; 99 | while (count <= 10) { 100 | const product = number * count; 101 | console.log(`${number} * ${count} = ${product}`); 102 | count = count + 1; 103 | } 104 | ``` 105 | **Output** 106 | ``` 107 | Enter a number: 9 108 | 9 * 1 = 9 109 | 9 * 2 = 18 110 | 9 * 3 = 27 111 | 9 * 4 = 36 112 | 9 * 5 = 45 113 | 9 * 6 = 54 114 | 9 * 7 = 63 115 | 9 * 8 = 72 116 | 9 * 9 = 81 117 | 9 * 10 = 90 118 | ``` 119 | *** 120 | ## do While Loop 121 | ### Example 1 122 | ```js 123 | let count = 1; 124 | do { 125 | console.log(count); 126 | count = count + 1; 127 | } while (count < 5) 128 | ``` 129 | **Output** 130 | ``` 131 | 1 132 | 2 133 | 3 134 | 4 135 | ``` 136 | *** 137 | 138 | ## Programming Task 139 | **Can you use the while loop to print the multiplication table for the given number. But this time, you have to print the number from 10 to 1. So the output would be something like this.** 140 | ```js 141 | 9 * 10 = 90 142 | 9 * 9 = 81 143 | 9 * 8 = 72 144 | 9 * 7 = 63 145 | 9 * 6 = 54 146 | 9 * 5 = 45 147 | 9 * 4 = 36 148 | 9 * 3 = 27 149 | 9 * 2 = 18 150 | 9 * 1 = 9 151 | ``` 152 | ### Solution: 153 | ```js 154 | const number = parseInt(prompt("Enter a number: ")); 155 | let count = 10; 156 | while (count >= 1) { 157 | const product = number * count; 158 | console.log(`${number} * ${count} = ${product}`); 159 | count = count - 1; 160 | } 161 | ``` 162 | **Output** 163 | ``` 164 | Enter a number: 9 165 | 9 * 10 = 90 166 | 9 * 9 = 81 167 | 9 * 8 = 72 168 | 9 * 7 = 63 169 | 9 * 6 = 54 170 | 9 * 5 = 45 171 | 9 * 4 = 36 172 | 9 * 3 = 27 173 | 9 * 2 = 18 174 | 9 * 1 = 9 175 | ``` 176 | *** 177 | ## Programiz Quiz 178 | **Q. Which of the following causes an infinite loop?** 179 | 180 | 1. while (true) {...} 181 | 2. let i = 3; 182 | while (i < 4) {...} 183 | 3. let i = 3; 184 | do { 185 | ...} while(i < 4) 186 | 4. All of the above 187 | 188 | Answer: 4 189 | -------------------------------------------------------------------------------- /12. for loop.md: -------------------------------------------------------------------------------- 1 | # 12. for loop 2 | ## for loop Example 3 | ```js 4 | for (let i = 1; i <= 5; i++) { 5 | console.log("Learning for loop."); 6 | console.log("JavaScript is fun."); 7 | 8 | console.log(i); 9 | } 10 | ``` 11 | **Output** 12 | ``` 13 | Learning for loop. 14 | JavaScript is fun. 15 | 1 16 | Learning for loop. 17 | JavaScript is fun. 18 | 2 19 | Learning for loop. 20 | JavaScript is fun. 21 | 3 22 | Learning for loop. 23 | JavaScript is fun. 24 | 4 25 | Learning for loop. 26 | JavaScript is fun. 27 | 5 28 | ``` 29 | *** 30 | ## Creating Multiplication Table 31 | ```js 32 | const number = parseInt(prompt("Enter a number: ")); 33 | for (let count = 1; count <= 10; count++) { 34 | const product = number * count; 35 | console.log(`${number} * ${count} = ${product}`); 36 | } 37 | ``` 38 | **Output** 39 | ``` 40 | Enter a number: 9 41 | 9 * 1 = 9 42 | 9 * 2 = 18 43 | 9 * 3 = 27 44 | 9 * 4 = 36 45 | 9 * 5 = 45 46 | 9 * 6 = 54 47 | 9 * 7 = 63 48 | 9 * 8 = 72 49 | 9 * 9 = 81 50 | 9 * 10 = 90 51 | ``` 52 | *** 53 | ## for...in Loop Example 54 | ```js 55 | const student = { 56 | name: 'Monica', 57 | class: 7, 58 | age: 12 59 | } 60 | 61 | for ( let data in student ) { 62 | console.log(`${data} => ${student[data]}`); 63 | } 64 | ``` 65 | **Output** 66 | ``` 67 | name => Monica 68 | class => 7 69 | age => 12 70 | ``` 71 | *** 72 | ## for of loop 73 | ```js 74 | const students = ['John', 'Sara', 'Jack']; 75 | for ( let data of students) { 76 | console.log(data); 77 | } 78 | ``` 79 | **Output** 80 | ``` 81 | John 82 | Sara 83 | Jack 84 | ``` 85 | *** 86 | ### Get Character 87 | ```js 88 | let language = "JavaScript"; 89 | for (let character of language) { 90 | console.log(character); 91 | } 92 | ``` 93 | **Output** 94 | ``` 95 | J 96 | a 97 | v 98 | a 99 | S 100 | c 101 | r 102 | i 103 | p 104 | t 105 | ``` 106 | ## Programming Task 107 | **Can you use the for loop to print all the even numbers from 1 to 100. So the output would be something like this.** 108 | ```js 109 | 2 110 | 4 111 | 6 112 | 8 113 | 10 114 | 12 115 | ........ 116 | ``` 117 | ### Solution: 118 | ```js 119 | for (let i = 1; i <= 100; i++) { 120 | if(i % 2 == 0) { 121 | console.log(i); 122 | } 123 | } 124 | ``` 125 | **Output** 126 | ``` 127 | 2 128 | 4 129 | 6 130 | 8 131 | 10 132 | 12 133 | 14 134 | 16 135 | 18 136 | 20 137 | 22 138 | 24 139 | 26 140 | 28 141 | 30 142 | 32 143 | 34 144 | 36 145 | 38 146 | 40 147 | 42 148 | 44 149 | 46 150 | 48 151 | 50 152 | 52 153 | 54 154 | 56 155 | 58 156 | 60 157 | 62 158 | 64 159 | 66 160 | 68 161 | 70 162 | 72 163 | 74 164 | 76 165 | 78 166 | 80 167 | 82 168 | 84 169 | 86 170 | 88 171 | 90 172 | 92 173 | 94 174 | 96 175 | 98 176 | 100 177 | ``` 178 | *** 179 | ## Programiz Quiz 180 | **Q. Which of the following loops is used to iterate over keys of an object?** 181 | 182 | 1. for each loop 183 | 2. for...in loop 184 | 3. for of loop 185 | 4. for loop 186 | 187 | 188 | Answer: 2 189 | -------------------------------------------------------------------------------- /13. break and continue.md: -------------------------------------------------------------------------------- 1 | # 13. break and continue 2 | ## for loop without break Example 3 | ```js 4 | for (let i = 1; i <= 5; i++) { 5 | console.log(i); 6 | } 7 | ``` 8 | **Output** 9 | ``` 10 | 1 11 | 2 12 | 3 13 | 4 14 | 5 15 | ``` 16 | *** 17 | ## for loop with break Example 18 | ```js 19 | for (let i = 1; i <= 5; i++) { 20 | console.log(i); 21 | break; 22 | } 23 | ``` 24 | **Output** 25 | ``` 26 | 1 27 | ``` 28 | *** 29 | ## break with specific condition 30 | ```js 31 | for (let i = 1; i <= 5; i++) { 32 | if (i == 3) { 33 | break; 34 | } 35 | console.log(i); 36 | } 37 | ``` 38 | **Output** 39 | ``` 40 | 1 41 | 2 42 | ``` 43 | *** 44 | ## break with while 45 | ```js 46 | while (true) { 47 | let number = parseFloat(prompt("Enter a number: ")); 48 | if (number < 0) { 49 | break; 50 | } 51 | console.log(number); 52 | } 53 | ``` 54 | **Output** 55 | ``` 56 | Enter a number: 5 57 | 5 58 | Enter a number: 9 59 | 9 60 | Enter a number: -4 61 | -4 62 | ``` 63 | *** 64 | ### continue Statement 65 | ```js 66 | for (let i = 1; i <= 5; i++) { 67 | 68 | if (i == 3) { 69 | continue; 70 | } 71 | 72 | console.log(i); 73 | } 74 | ``` 75 | **Output** 76 | ``` 77 | 1 78 | 2 79 | 4 80 | 5 81 | ``` 82 | ## break and continue with while 83 | ```js 84 | while (true) { 85 | let number = parseFloat(prompt("Enter a number: ")); 86 | if (number < 0) { 87 | break; 88 | } 89 | if (number % 2 != 0) { 90 | continue; 91 | } 92 | 93 | console.log(number); 94 | } 95 | ``` 96 | **Output** 97 | ``` 98 | Enter a number: 4 99 | 4 100 | Enter a number: 9 101 | Enter a number: 28 102 | 28 103 | Enter a number: -34 104 | ``` 105 | ## Programming Task 106 | **Can you create a program that takes the input from the user. If the user enters a prime number, print the prime number. If the user enters a negative or non-prime number, ask the user for another number. And when the user enters a number greater than 100, terminate the loop.** 107 | ### Solution: 108 | ```js 109 | while (true) { 110 | let number = parseFloat(prompt("Enter a number: ")); 111 | if(number > 100) { 112 | break; 113 | } 114 | 115 | let isPrime = true; 116 | for (let i = 2; i < number; i++) { 117 | if (number % i == 0) { 118 | isPrime = false; 119 | break; 120 | } 121 | } 122 | 123 | if (number < 0 || !isPrime) { 124 | continue; 125 | } 126 | 127 | else if (isPrime) { 128 | console.log(number); 129 | } 130 | } 131 | ``` 132 | **Output** 133 | ``` 134 | Enter a number: 5 135 | Enter a number: 8 136 | Enter a number: 11 137 | Enter a number: 111 138 | 5 139 | 11 140 | ``` 141 | *** 142 | ## Programiz Quiz 143 | **Q. Which of the following keywords is used to terminate a loop?** 144 | 145 | 1. terminate 146 | 2. break 147 | 3. continue 148 | 4. loop 149 | 150 | Answer: 2 151 | 152 | -------------------------------------------------------------------------------- /14. JavaScript Functions.md: -------------------------------------------------------------------------------- 1 | # JavaScript Functions 2 | ### Function Example 1 3 | 4 | ```js 5 | function greet() { 6 | console.log("Hello there"); 7 | } 8 | greet(); 9 | ``` 10 | **Output** 11 | ``` 12 | Hello there 13 | ``` 14 | *** 15 | ### Function Example 2 16 | ```js 17 | function greet() { 18 | console.log("Hello there"); 19 | } 20 | greet(); 21 | console.log("After function call."); 22 | ``` 23 | **Output** 24 | ``` 25 | Hello there 26 | After function call 27 | ``` 28 | *** 29 | ### Function Example 3 30 | ```js 31 | function greet() { 32 | console.log("Hello there"); 33 | } 34 | greet(); 35 | greet(); 36 | greet(); 37 | ``` 38 | **Output** 39 | ``` 40 | Hello there 41 | Hello there 42 | Hello there 43 | ``` 44 | *** 45 | ## Function arguments 46 | ### Function Arguments Example 1 47 | ```js 48 | function greet(name) { 49 | console.log("Hello " + name); 50 | } 51 | 52 | greet(); 53 | ``` 54 | **Output** 55 | ``` 56 | Hello undefined 57 | ``` 58 | *** 59 | ### Function Arguments Example 2 60 | ```js 61 | function greet(name) { 62 | console.log("Hello " + name); 63 | } 64 | 65 | greet("Jude"); 66 | ``` 67 | **Output** 68 | ``` 69 | Hello Jude 70 | ``` 71 | *** 72 | ### Function Arguments Example 3 73 | ```js 74 | function greet(name) { 75 | console.log("Hello " + name); 76 | } 77 | 78 | greet("Jude"); 79 | greet("Jack"); 80 | ``` 81 | **Output** 82 | ``` 83 | Hello Jude 84 | Hello Jack 85 | ``` 86 | *** 87 | ## Passing Multiple Arguments 88 | ```js 89 | function addNumbers(n1, n2) { 90 | let result = n1 + n2; 91 | console.log("The sum is " + result); 92 | } 93 | 94 | let number1 = 6.6; 95 | let number2 = 2.5; 96 | addNumbers(number1, number2); 97 | ``` 98 | **Output** 99 | ``` 100 | The sum is 9.1 101 | ``` 102 | *** 103 | ## Return Value from a Function 104 | ```js 105 | function addNumbers(n1, n2) { 106 | let result = n1 + n2; 107 | return result; 108 | } 109 | 110 | let sum = addNumbers(6.6, 2.5); 111 | console.log("The sum is " + sum); 112 | ``` 113 | **Output** 114 | ``` 115 | The sum is 9.1 116 | ``` 117 | ## JavaScript Built-in Functions 118 | ```js 119 | const num = 9; 120 | const squareRoot = Math.sqrt(num); 121 | console.log(`The square root of ${num} is ${squareRoot}`); 122 | ``` 123 | **Output** 124 | ``` 125 | The square root of 9 is 3 126 | ``` 127 | *** 128 | ## Programming Task 129 | **Can you create a program that can tell us if someone is eligible to vote? For this, 130 | Create a function canVote that accepts the age of the person. 131 | If the age is above 18, then return true else false 132 | Call the function for ages 17, 20 and 65 to verify if it is working correctly or not.** 133 | ### Solution: 134 | ```js 135 | function canVote(age) { 136 | if (age > 18) { 137 | return true; 138 | } else { 139 | return false; 140 | } 141 | } 142 | 143 | const result1 = canVote(17); 144 | console.log(result1); 145 | const result2 = canVote(20); 146 | console.log(result2); 147 | const result3 = canVote(65); 148 | console.log(result3); 149 | ``` 150 | **Output** 151 | ``` 152 | false 153 | true 154 | true 155 | ``` 156 | *** 157 | ### Programiz Quiz 158 | **Q. What will be the output of the following code?** 159 | ```js 160 | console.log(sqrt(9)); 161 | ``` 162 | 1. 3 163 | 2. 81 164 | 3. error 165 | 4. None of the above 166 | 167 | Answer: 1 168 | 169 | 170 | 171 | -------------------------------------------------------------------------------- /15. Arrow functions.md: -------------------------------------------------------------------------------- 1 | # JavaScript Arrow Functions 2 | ### Example 1 3 | ```js 4 | function greet() { 5 | console.log("Good Morning"); 6 | } 7 | 8 | greet(); 9 | ``` 10 | **Output** 11 | ``` 12 | Good Morning 13 | ``` 14 | *** 15 | ### Example 2 16 | ```js 17 | const greet = () => console.log("Good Morning"); 18 | 19 | greet(); 20 | ``` 21 | **Output** 22 | ``` 23 | Good Morning 24 | ``` 25 | *** 26 | ### Example 3 27 | ```js 28 | const greet = () => { 29 | console.log("Good Morning"); 30 | console.log("How are you?"); 31 | } 32 | 33 | greet(); 34 | ``` 35 | **Output** 36 | ``` 37 | Good Morning 38 | How are you? 39 | ``` 40 | *** 41 | ## Arrow function with Argument 42 | ### Example 1 43 | ```js 44 | let addNumbers = (a, b) => console.log(a + b); 45 | 46 | addNumbers(5, 9); 47 | ``` 48 | **Output** 49 | ``` 50 | 14 51 | ``` 52 | *** 53 | ### Example 2 54 | ```js 55 | let addNumbers = a => console.log(a ** 2); 56 | 57 | addNumbers(5, 9); 58 | ``` 59 | **Output** 60 | ``` 61 | 25 62 | ``` 63 | ## Arrow Function with return statement 64 | ```js 65 | let addNumbers = (a, b) => { 66 | let result = a + b; 67 | return result; 68 | } 69 | let sum = addNumbers(5, 8); 70 | console.log("The sum is " + sum); 71 | ``` 72 | **Output** 73 | ``` 74 | The sum is 13. 75 | ``` 76 | *** 77 | ```js 78 | let addNumbers = (a, b) => { 79 | let result = a + b; 80 | return result; 81 | console.log("After Return"); 82 | } 83 | let sum = addNumbers(5, 8); 84 | console.log("The sum is " + sum); 85 | ``` 86 | **Output** 87 | ``` 88 | Result = 17 89 | ``` 90 | *** 91 | ## Programming Task 92 | **Can you create a program using an arrow function to check if the number entered by the user is a prime number or not. This function should compute the result and return them to the function call. And, the results should be printed from outside the function.** 93 | ### Solution: 94 | ```js 95 | let findPrime = (number) => { 96 | let result = true; 97 | for (let i = 2; i <= number -1; i++) { 98 | if(number % i == 0) { 99 | result = false; 100 | break; 101 | } 102 | } 103 | return result; 104 | } 105 | 106 | const userValue = prompt(parseInt("Enter a positive number: ")); 107 | let checkResult = findPrime(userValue); 108 | if(checkResult) { 109 | console.log("The number is prime."); 110 | } else { 111 | console.log("The number is not prime."); 112 | } 113 | ``` 114 | **Output** 115 | ``` 116 | Enter a positive number: 435 117 | The number is not prime. 118 | ``` 119 | *** 120 | ## Programiz Quiz 121 | **Q. What is the correct way to call this arrow function?** 122 | ```js 123 | let calculatePrime = (number) => {...} 124 | ``` 125 | 1. (5) => 126 | 2. =>(5); 127 | 3. calculatePrime(5) => 128 | 4. calculatePrime(5); 129 | 130 | Answer: 4 131 | 132 | 133 | -------------------------------------------------------------------------------- /16. Global and local scope.md: -------------------------------------------------------------------------------- 1 | ## Scope 2 | ```js 3 | let number = 5; 4 | console.log(number); 5 | ``` 6 | **Output** 7 | ``` 8 | 5 9 | ``` 10 | *** 11 | ## Local Scope 12 | ### Example 1 13 | ```js 14 | function addNumbers (n1, n2) { 15 | let result = n1 + n2; 16 | console.log(result); 17 | } 18 | 19 | addNumbers(4, 9); 20 | ``` 21 | **Output** 22 | ``` 23 | 13 24 | ``` 25 | *** 26 | ### Example 2 27 | ```js 28 | function addNumbers (n1, n2) { 29 | let result = n1 + n2; 30 | console.log(result); 31 | } 32 | 33 | addNumbers(4, 9); 34 | console.log(result); 35 | ``` 36 | **Output** 37 | ``` 38 | ReferenceError: Can't find variable: result 39 | ``` 40 | *** 41 | ### Example 3 42 | ```js 43 | function addNumbers (n1, n2) { 44 | let result = n1 + n2; 45 | console.log(result); 46 | } 47 | 48 | addNumbers(4, 9); 49 | 50 | let result = 123; 51 | console.log(result); 52 | ``` 53 | **Output** 54 | ``` 55 | 13 56 | 123 57 | ``` 58 | *** 59 | ## Global Scope 60 | ### Example 1 61 | ```js 62 | let result; 63 | 64 | function addNumbers (n1, n2) { 65 | result = n1 + n2; 66 | } 67 | 68 | addNumbers(4, 9); 69 | console.log(result); 70 | ``` 71 | **Output** 72 | ``` 73 | 13 74 | ``` 75 | *** 76 | ### Change the value of Global Variable 77 | ```js 78 | let message = "hello"; 79 | 80 | function greet() { 81 | message = "How are you?" 82 | 83 | } 84 | 85 | // before the function call 86 | console.log(message); 87 | 88 | greet(); 89 | 90 | //after the function call 91 | console.log(message); 92 | ``` 93 | **Output** 94 | ``` 95 | hello 96 | How are you? 97 | ``` 98 | *** 99 | ## Block Scoped 100 | ### Example 1 101 | ```js 102 | const addNumbers = (n1, n2) => { 103 | let result = n1 + n2; 104 | console.log(result); 105 | if (result > 10) { 106 | let result1 = "Positive"; 107 | console.log(result1) 108 | } 109 | } 110 | 111 | addNumbers(4, 9); 112 | ``` 113 | **Output** 114 | ``` 115 | 13 116 | Positive 117 | ``` 118 | *** 119 | ### Example 2 120 | ```js 121 | const addNumbers = (n1, n2) => { 122 | let result = n1 + n2; 123 | console.log(result); 124 | if (result > 10) { 125 | let result1 = "Positive"; 126 | console.log(result1) 127 | } 128 | console.log(result1); 129 | } 130 | 131 | addNumbers(4, 9); 132 | ``` 133 | **Output** 134 | ``` 135 | 13 136 | Positive 137 | ReferenceError: Can't find variable: result1 138 | ``` 139 | *** 140 | ### Programiz Quiz 141 | **Q. What is the output of the program?** 142 | ```js 143 | let a = 88; 144 | function checkValue() { 145 | let a = 77; 146 | a = a - 57; 147 | } 148 | checkValue(); 149 | console.log(a); 150 | ``` 151 | 1. 88 152 | 2. 77 153 | 3. 20 154 | 4. 31 155 | 156 | Answer: 1 157 | -------------------------------------------------------------------------------- /17. JavaScript Objects.md: -------------------------------------------------------------------------------- 1 | # JavaScript Objects 2 | 3 | ```js 4 | const person = { 5 | name: "John", 6 | age: 20, 7 | }; 8 | console.log(person); 9 | ``` 10 | **Output** 11 | ``` 12 | {name: "John", age: 20} 13 | ``` 14 | 15 | ## To find Type 16 | ```js 17 | const person = { 18 | name: "John", 19 | age: 20, 20 | }; 21 | console.log(person); 22 | console.log(typeof(person)); 23 | ``` 24 | **Output** 25 | ``` 26 | {name: "John", age: 20} 27 | object 28 | ``` 29 | ## Accessing Object Properties 30 | ### Using dot notation 31 | ```js 32 | const person = { 33 | name: "John", 34 | age: 20, 35 | }; 36 | console.log(person.name); 37 | console.log(person.age); 38 | ``` 39 | **Output** 40 | ``` 41 | John 42 | 20 43 | ``` 44 | ## Change the value of an object 45 | ```js 46 | const person = { 47 | name: "John", 48 | age: 20, 49 | student: true 50 | }; 51 | console.log(person.name); 52 | console.log(person.age); 53 | 54 | person.age = 29; 55 | console.log(person.age); 56 | ``` 57 | **Output** 58 | ``` 59 | John 60 | 20 61 | 29 62 | ``` 63 | ### Using bracket notation 64 | ```js 65 | const person = { 66 | name: "John", 67 | age: 20, 68 | }; 69 | console.log(person["name"]); 70 | console.log(person["age"]); 71 | ``` 72 | **Output** 73 | ``` 74 | John 75 | 20 76 | ``` 77 | ## Object Methods 78 | ```js 79 | const person = { 80 | name: "John", 81 | age: 30, 82 | greet: function() { console.log('hello') } 83 | } 84 | person.greet(); 85 | ``` 86 | **Output** 87 | ``` 88 | hello 89 | ``` 90 | *** 91 | ## Programming Task 92 | **Can you create an object named student with keys name, rollNo, totalMarks. Give any values you prefer. 93 | Also, create two functions: first function to print the information about the student and a second function to check if the student passed the exam or not. If the totalMarks is less than 40, print 'You failed'. If the totalMarks is greater than or equal to 40, print 'You passed'.** 94 | ### Solution: 95 | ```js 96 | const student = { 97 | name: 'Jack', 98 | rollNo: 12, 99 | total_marks : 40 100 | } 101 | 102 | function studentInfo() { 103 | console.log(student.name); 104 | console.log(student.rollNo); 105 | console.log(student.total_marks); 106 | } 107 | 108 | function checkResult() { 109 | if (student.total_marks < 40) { 110 | console.log('You failed.') 111 | } else { 112 | console.log('You passed.') 113 | } 114 | } 115 | 116 | studentInfo(); 117 | checkResult(); 118 | ``` 119 | 120 | **Output** 121 | ``` 122 | Jack 123 | 12 124 | 40 125 | You passed. 126 | ``` 127 | ## Programiz Quiz 128 | **Q. What is the correct way to call this access the object value name?** 129 | ```js 130 | const student = { 131 | name: "Sarah", 132 | class: 10 133 | } 134 | ``` 135 | 1. student[name] 136 | 2. student.name 137 | 3. student.name() 138 | 4. student["name"]() 139 | 140 | Answer: 2 141 | -------------------------------------------------------------------------------- /18. Strings.md: -------------------------------------------------------------------------------- 1 | # Strings 2 | ## Example 1 3 | ```js 4 | let text = "JavaScript is fun"; 5 | console.log(text); 6 | console.log(typeof(text)); 7 | ``` 8 | **Output** 9 | ``` 10 | JavaScript is fun 11 | string 12 | ``` 13 | *** 14 | ## Example 2 15 | ```js 16 | let age = 25; 17 | let text = `I am ${age} years old`; 18 | console.log(text); 19 | console.log(typeof(text)); 20 | ``` 21 | **Output** 22 | ``` 23 | I am 25 years old 24 | string 25 | ``` 26 | *** 27 | ## Access String Characters 28 | ```js 29 | let text = "Hello"; 30 | console.log(text[0]); 31 | console.log(text[1]); 32 | console.log(text[2]); 33 | ``` 34 | **Output** 35 | ``` 36 | H 37 | e 38 | l 39 | ``` 40 | *** 41 | ## JavaScript String Length 42 | ```js 43 | let text = "Hello"; 44 | console.log(text.length); 45 | ``` 46 | **Output** 47 | ``` 48 | 5 49 | ``` 50 | *** 51 | ## charAt() Method 52 | ```js 53 | let text = "Hello"; 54 | console.log(text.charAt(0)); 55 | console.log(text.charAt(1)); 56 | console.log(text.charAt(2)); 57 | ``` 58 | **Output** 59 | ``` 60 | H 61 | e 62 | l 63 | ``` 64 | *** 65 | ## JavaScript String is Case-Sensitive 66 | ```js 67 | let str1 = "HELLO"; 68 | let str2 = "hello"; 69 | 70 | console.log(str1 === str2); 71 | ``` 72 | **Output** 73 | ``` 74 | false 75 | ``` 76 | *** 77 | ## JavaScript Multiline Strings 78 | ```js 79 | const message = "Hello everyone. \ 80 | Welcome to our JavaScript tutorial. \ 81 | In this tutorial, you will learn about \ 82 | JavaScript in a fun and easy way."; 83 | 84 | console.log(message); 85 | ``` 86 | **Output** 87 | ``` 88 | Hello everyone. Welcome to our JavaScript tutorial. In this tutorial, you will learn about JavaScript in a fun and easy way. 89 | ``` 90 | 91 | *** 92 | ## Programming Task 93 | **Create a JavaScript string and find the first and last character of the string using the charAt() method. 94 | Also find the length of the string.** 95 | ### Solution: 96 | ```js 97 | const message = "Hello everyone. \ 98 | Welcome to our JavaScript tutorial. \ 99 | In this tutorial, you will learn about \ 100 | JavaScript in a fun and easy way."; 101 | 102 | // length of the string 103 | const totalLength = message.length; 104 | console.log(`Length: ${totalLength}`) 105 | 106 | // first character 107 | console.log(message.charAt(0)); 108 | 109 | //last character 110 | console.log(message.charAt(totalLength-1)); 111 | ``` 112 | **Output** 113 | ``` 114 | Length: 136 115 | H 116 | . 117 | ``` 118 | *** 119 | ## Programiz Quiz 120 | **Q. What is the output of the program?** 121 | ```js 122 | let message = "JavaScript"; 123 | let len = message.length; 124 | 125 | console.log(message.charAt(len - 2)); 126 | ``` 127 | 1. Error 128 | 2. p 129 | 3. t 130 | 4. J 131 | 132 | Answer: 2 133 | -------------------------------------------------------------------------------- /19. 10 String Methods_Properties You Must know.md: -------------------------------------------------------------------------------- 1 | # 19. 10 String Methods/Properties You Must know 2 | ## 1. toUpperCase() 3 | ```js 4 | const text = "I like javascript"; 5 | const result = text.toUpperCase(); 6 | 7 | console.log(result); 8 | ``` 9 | **Output** 10 | ``` 11 | I LIKE JAVASCRIPT 12 | ``` 13 | *** 14 | ## 2. toLowerCase() Method 15 | ```js 16 | const text = "I LIKE JAVASCRIPT"; 17 | const result = text.toLowerCase(); 18 | 19 | console.log(result); 20 | ``` 21 | **Output** 22 | ``` 23 | i like javascript 24 | ``` 25 | *** 26 | ## 3. concat() 27 | ```js 28 | const text1 = "JavaScript"; 29 | const text2 = "Programming"; 30 | 31 | const result = text1.concat(text2); 32 | console.log(result); 33 | ``` 34 | **Output** 35 | ``` 36 | JavaScriptProgramming 37 | ``` 38 | *** 39 | ## 4. replace() 40 | ```js 41 | const text = "Hello. My name is Punit." 42 | console.log(text); 43 | 44 | const result = text.replace("Hello", "Hi"); 45 | console.log(result); 46 | ``` 47 | **Output** 48 | ``` 49 | Hi. My name is Punit. 50 | ``` 51 | *** 52 | ## 5. split() 53 | ```js 54 | const text = "hello"; 55 | const result = text.split(''); 56 | console.log(result); 57 | ``` 58 | **Output** 59 | ``` 60 | ["h", "e", "l", "l", "o"] 61 | ``` 62 | *** 63 | ## 6. slice() 64 | ```js 65 | const text = "JavaScript"; 66 | const result = text.slice(0,4); 67 | console.log(result); 68 | ``` 69 | **Output** 70 | ``` 71 | Java 72 | ``` 73 | *** 74 | ## 7. trim() 75 | ```js 76 | const text = " JavaScript "; 77 | const result = text.trim(); 78 | console.log(result); 79 | ``` 80 | **Output** 81 | ``` 82 | JavaScript 83 | ``` 84 | *** 85 | ## 8. search() 86 | ### Example 1 87 | ```js 88 | const text = "JavaScript is fun."; 89 | const result = text.search("fun"); 90 | console.log(result); 91 | ``` 92 | **Output** 93 | ``` 94 | 14 95 | ``` 96 | *** 97 | ### Example 2 98 | ```js 99 | const text = "JavaScript is fun."; 100 | const result = text.search("funny"); 101 | console.log(result); 102 | ``` 103 | **Output** 104 | ``` 105 | -1 106 | ``` 107 | *** 108 | ## Programming Task 109 | **Create a string "I love Java". Convert the string to lowercase and check if the string consists of the substring "java". If yes, replace the substring "java" with "JavaScript".** 110 | ### Solution: 111 | ```js 112 | const text = "I love Java."; 113 | 114 | const smallcase = text.toLowerCase(); 115 | 116 | const checkValue = smallcase.includes('java'); 117 | 118 | if(checkValue) { 119 | const result = smallcase.replace('java', 'JavaScript'); 120 | console.log(result); 121 | } 122 | ``` 123 | **Output** 124 | ``` 125 | i love JavaScript. 126 | ``` 127 | *** 128 | ## Programiz Quiz 129 | **Q. What is the output of the program?** 130 | ```js 131 | const message = "JavaScript is fun"; 132 | const result = message.split(' '); 133 | 134 | const value = result[0].charAt(2); 135 | console.log(value); 136 | ``` 137 | 138 | 1. s 139 | 2. v 140 | 3. is 141 | 4. fun 142 | 143 | Answer: 2 144 | 145 | -------------------------------------------------------------------------------- /2. JavaScript Variables.md: -------------------------------------------------------------------------------- 1 | # 2. JavaScript Variables 2 | *** 3 | JavaScript is one of the most popular and dynamic programming languages out there. 4 | ## Programs in the Videos 5 | 6 | ## Strings 7 | ```js 8 | console.log("I love JavaScript"); 9 | console.log('JavaScript is fun'); 10 | ``` 11 | **Output** 12 | 13 | ``` 14 | I love JavaScript 15 | JavaScript is fun 16 | ``` 17 | ```js 18 | console.log(`I love JavaScript`); 19 | console.log(`JavaScript is fun`); 20 | ``` 21 | **Output** 22 | 23 | ``` 24 | I love JavaScript 25 | JavaScript is fun 26 | ``` 27 | ## Numbers 28 | ```js 29 | console.log(8); 30 | console.log(80.5); 31 | ``` 32 | **Output** 33 | 34 | ``` 35 | 8 36 | 80.5 37 | 38 | ``` 39 | ## JavaScript Variables 40 | ```js 41 | let language = "JavaScript"; 42 | console.log(language); 43 | ``` 44 | **Output** 45 | 46 | ``` 47 | JavaScript 48 | ``` 49 | *** 50 | ```js 51 | let number = 5; 52 | console.log(number); 53 | ``` 54 | **Output** 55 | 56 | ``` 57 | 5 58 | ``` 59 | *** 60 | ```js 61 | var name = "Jack"; 62 | console.log(name); 63 | ``` 64 | **Output** 65 | 66 | ``` 67 | Jack 68 | ``` 69 | *** 70 | ```js 71 | let name = "Punit" 72 | console.log(name) 73 | name = "James" 74 | console.log(name) 75 | ``` 76 | **Output** 77 | ``` 78 | Punit 79 | James 80 | ``` 81 | ## Variable Using const 82 | ```js 83 | const passportNumber = 39983; 84 | console.log(passportNumber); 85 | ``` 86 | **Output** 87 | ``` 88 | 39983 89 | ``` 90 | ## Changing Value in const 91 | ```js 92 | const passportNumber = 39983; 93 | console.log(passportNumber); 94 | 95 | passportNumber = 44325; 96 | console.log(passportNumber); 97 | ``` 98 | **Output** 99 | ``` 100 | TypeError: Assignment to constant variable. 101 | ``` 102 | ## JavaScript undefined 103 | ```js 104 | let name; 105 | console.log(name); 106 | ``` 107 | **Output** 108 | ``` 109 | undefined 110 | ``` 111 | *** 112 | ```js 113 | let name = "Punit"; 114 | console.log(name); 115 | 116 | name = undefined; 117 | console.log(name); 118 | ``` 119 | **Output** 120 | ``` 121 | Punit 122 | undefined 123 | ``` 124 | *** 125 | ## Print variables and string in single line 126 | ```js 127 | let city = "New York"; 128 | console.log("City: " + city); 129 | ``` 130 | **Output** 131 | ``` 132 | City: New York 133 | ``` 134 | ## Template Literal 135 | ```js 136 | let city = "New York"; 137 | console.log(`City: ${city}`); 138 | ``` 139 | **Output** 140 | ``` 141 | City: New York 142 | ``` 143 | *** 144 | ```js 145 | let city = "New York"; 146 | let kfcLocation = 10; 147 | console.log("City: " + city + ", " + "KFC Locations :" + kfcLocation); 148 | ``` 149 | **Output** 150 | ``` 151 | City: New York, KFC Locations :10 152 | ``` 153 | ## Another Example 154 | ```js 155 | let city = "New York"; 156 | let kfcLocation = 10; 157 | 158 | console.log(`City: ${city}, KFC Locations: ${kfcLocation}`); 159 | ``` 160 | **Output** 161 | ``` 162 | City: New York, KFC Locations :10 163 | ``` 164 | *** 165 | ## Programmiz Quiz 166 | **Q. Which of the following is the correct variable name?** 167 | 168 | 1. switchNumber 169 | 2. 1switchNumber 170 | 3. switch number 171 | 4. switch 172 | 173 | Answer: 1 174 | 175 | -------------------------------------------------------------------------------- /20. JavaScript Array.md: -------------------------------------------------------------------------------- 1 | # JavaScript Array 2 | 3 | ```js 4 | const name1 = "Jack"; 5 | const name2 = "Sarah"; 6 | const name3 = "Philip"; 7 | ... 8 | ... 9 | ... 10 | const name100 = "Amy"; 11 | ``` 12 | *** 13 | ## Create an Array 14 | ```js 15 | const routine = ['eat', 'sleep']; 16 | console.log(routine); 17 | ``` 18 | **Output** 19 | ``` 20 | [ 'eat', 'sleep' ] 21 | ``` 22 | *** 23 | 24 | ```js 25 | const numbers = [2, 3, 5, 7]; 26 | console.log(numbers); 27 | ``` 28 | **Output** 29 | ``` 30 | [ 2, 3, 5, 7 ] 31 | ``` 32 | *** 33 | ```js 34 | const arr = ['eat', 'sleep', 3, 5, 'run']; 35 | console.log(arr); 36 | ``` 37 | **Output** 38 | ``` 39 | [ 'eat', 'sleep', 3, 5, 'run' ] 40 | ``` 41 | ## Access Elements of an Array 42 | ```js 43 | const myArray = ['h', 'e', 'l', 'l', 'o']; 44 | console.log(myArray[0]); 45 | ``` 46 | **Output** 47 | ``` 48 | h 49 | ``` 50 | *** 51 | 52 | ```js 53 | const myArray = ['h', 'e', 'l', 'l', 'o']; 54 | console.log(myArray[0]); 55 | console.log(myArray[1]); 56 | console.log(myArray[2]); 57 | ``` 58 | **Output** 59 | ``` 60 | h 61 | e 62 | l 63 | ``` 64 | *** 65 | ## Array length 66 | ```js 67 | const routine = [ 'eat', 'sleep']; 68 | console.log(routine.length); 69 | ``` 70 | **Output** 71 | ``` 72 | 2 73 | ``` 74 | *** 75 | ## Add an Element to an Array 76 | ```js 77 | let routine = ['eat', 'sleep']; 78 | 79 | // add an element at the end 80 | routine.push('exercise'); 81 | console.log(routine); 82 | ``` 83 | **Output** 84 | ``` 85 | ['eat', 'sleep', 'exercise'] 86 | ``` 87 | *** 88 | ## Change the Elements of an Array 89 | ```js 90 | let routine = ['eat', 'sleep']; 91 | 92 | routine[1] = 'exercise'; 93 | console.log(routine); 94 | ``` 95 | **Output** 96 | ``` 97 | [ 'eat', 'exercise' ] 98 | ``` 99 | *** 100 | ## Remove an Element from an Array 101 | ```js 102 | let routine = ['work', 'eat', 'sleep', 'exercise']; 103 | 104 | // remove the last element 105 | routine.pop(); 106 | console.log(routine); 107 | ``` 108 | **Output** 109 | ``` 110 | ['work', 'eat', 'sleep'] 111 | ``` 112 | *** 113 | ## Programming Task 114 | **Create an array named greet with values 'hello', 'hi'. 115 | Find the length of an array. 116 | Add the new element 'welcome' to the array. 117 | Find the new length of an array.** 118 | ### Solution: 119 | ```js 120 | const greet = ['hello', 'hi']; 121 | const greetLength = greet.length; 122 | console.log(greetLength); 123 | greet.push('welcome'); 124 | const addGreetLength = greet.length; 125 | console.log(addGreetLength); 126 | ``` 127 | **Output** 128 | ``` 129 | 2 130 | 3 131 | ``` 132 | *** 133 | ## Programiz Quiz 134 | **Q. What is the output of the program?** 135 | ```js 136 | let routine = [ 'eat', 'sleep']; 137 | routine.push('exercise'); 138 | routine[4] = 'work'; 139 | console.log(routine[3]); 140 | ``` 141 | 1. Error 142 | 2. exercise 143 | 3. undefined 144 | 4. work 145 | 146 | Answer: 3 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | -------------------------------------------------------------------------------- /21. Array Methods You Must Know.md: -------------------------------------------------------------------------------- 1 | # 8 Array Methods You Must Know 2 | ## 1. push() and unshift() Method 3 | ```js 4 | const fruits = ['apple', 'orange', 'mango']; 5 | 6 | // add element at the end of array 7 | fruits.push('pineapple'); 8 | console.log(fruits); 9 | ``` 10 | *** 11 | **Output** 12 | ``` 13 | ["apple", "orange", "mango", "pineapple"] 14 | ``` 15 | *** 16 | ```js 17 | const fruits = ['apple', 'orange', 'mango']; 18 | 19 | // add element at the end of array 20 | fruits.unshift('kiwi'); 21 | console.log(fruits); 22 | ``` 23 | **Output** 24 | ``` 25 | ["kiwi", "apple", "orange", "mango"] 26 | ``` 27 | *** 28 | ## 2. pop() and shift() Method 29 | ```js 30 | const fruits = ['apple', 'orange', 'mango']; 31 | fruits.pop(); 32 | console.log(fruits); 33 | ``` 34 | **Output** 35 | ``` 36 | ["apple", "orange"] 37 | ``` 38 | *** 39 | ```js 40 | const fruits = ['apple', 'orange', 'mango']; 41 | 42 | fruits.shift(); 43 | console.log(fruits); 44 | ``` 45 | **Output** 46 | ``` 47 | ["orange", "mango"] 48 | ``` 49 | ## 3. concat() Method 50 | ```js 51 | const array1 = ['hello', 'world']; 52 | const array2 = ['JavaScript', 'programming']; 53 | 54 | const result = array1.concat(array2); 55 | console.log(result); 56 | ``` 57 | **Output** 58 | ``` 59 | [ 'hello', 'world', 'JavaScript', 'programming' ] 60 | ``` 61 | *** 62 | ## 4. sort() Method 63 | ```js 64 | const fruits = [apple, mango, papaya, 'kiwi']; 65 | 66 | fruits.sort(); 67 | console.log(fruits); 68 | ``` 69 | **Output** 70 | ``` 71 | ["apple", "kiwi", "mango", "papaya"] 72 | ``` 73 | *** 74 | ## 5. slice() Method 75 | ```js 76 | const arrayValue = ['hello', 'world', 'JavaScript', 'programming']; 77 | 78 | const result = arrayValue.slice(0, 3); 79 | console.log(result); 80 | ``` 81 | **Output** 82 | ``` 83 | [ 'hello', 'world', 'JavaScript' ] 84 | ``` 85 | *** 86 | ```js 87 | const arrayValue = ['hello', 'world', 'JavaScript', 'programming']; 88 | 89 | const result = arrayValue.slice(2, 4); 90 | console.log(result); 91 | ``` 92 | **Output** 93 | ``` 94 | [ 'JavaScript', 'programming' ] 95 | ``` 96 | *** 97 | ## 6. splice() Method 98 | ```js 99 | const arrayValue = ['hello', 'world', 'JavaScript', 'programming']; 100 | 101 | arrayValue.splice(0, 2, 'language'); 102 | console.log(arrayValue); 103 | ``` 104 | **Output** 105 | ``` 106 | [ 'language', 'JavaScript', 'programming' ] 107 | ``` 108 | *** 109 | ## 7. includes() Method 110 | ```js 111 | const arrayValue = ['hello', 'world', 'JavaScript', 'programming']; 112 | 113 | const result = arrayValue.includes('JavaScript'); 114 | console.log(result); 115 | ``` 116 | **Output** 117 | ``` 118 | true 119 | ``` 120 | *** 121 | ```js 122 | const arrayValue = ['hello', 'world', 'JavaScript', 'programming']; 123 | 124 | const result = arrayValue.includes('Python'); 125 | console.log(result); 126 | ``` 127 | **Output** 128 | ``` 129 | false 130 | ``` 131 | ## 8. find() Method 132 | ```js 133 | const numbers = [2, 4, 7, 9, 12]; 134 | const result = numbers.find(element => element > 5); 135 | console.log(result); 136 | ``` 137 | **Output** 138 | ``` 139 | 7 140 | ``` 141 | ## Programming Task 142 | **Create an array value like this: 143 | ['I', 'Love', 'JavaScript']; 144 | Add the new value 'Programming' to the end of the array. 145 | Check if the array contains the text JavaScript. And if it does, print the text 'You are doing great. Keep learning.'** 146 | ### Solution: 147 | ```js 148 | const arrValue = ['I', 'Love', 'JavaScript']; 149 | arrValue.push('Programming'); 150 | const result = arrValue.includes('JavaScript'); 151 | if(result) { 152 | console.log('You are doing great. Keep learning.'); 153 | } 154 | ``` 155 | **Output** 156 | ``` 157 | You are doing great. Keep learning. 158 | ``` 159 | *** 160 | ## Programiz quiz 161 | **Q. What is the output of the program? You can see the options there.** 162 | ```js 163 | const message = ['JavaScript', 'is', 'really', 'fun']; 164 | message.sort(); 165 | const result = message.slice(0, 1); 166 | console.log(result); 167 | ``` 168 | 1. ['JavaScript'] 169 | 2. ['is'] 170 | 3. ['fun'] 171 | 4. ['really'] 172 | 173 | Answer: 1 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /22. map, filter, reduce.md: -------------------------------------------------------------------------------- 1 | # JavaScript Array 2 | ```js 3 | const number = [1, 2, 3, 4, 5]; 4 | ``` 5 | ## map() Method 6 | ```js 7 | const numbers = [1, 2, 3, 4, 5]; 8 | 9 | const square = numbers.map(item => item * item); 10 | console.log(square); 11 | ``` 12 | **Output** 13 | ``` 14 | [ 1, 4, 9, 16, 25 ] 15 | ``` 16 | *** 17 | ```js 18 | const names = ["James", "Marie", "Rosa"]; 19 | 20 | const fullNames = names.map(item => item + " Smith"); 21 | console.log(fullNames); 22 | ``` 23 | **Output** 24 | ``` 25 | [ 'James Smith', 'Marie Smith', 'Rosa Smith' ] 26 | ``` 27 | ## filter() Method 28 | ```js 29 | const number = [1, 2, 3, 4, 5]; 30 | 31 | const result = number.filter(item => item < 3); 32 | console.log(result); 33 | ``` 34 | **Output** 35 | ``` 36 | [1, 2] 37 | ``` 38 | *** 39 | ```js 40 | const number = [1, 2, 3, 4, 5]; 41 | 42 | const result = number.filter(item => item % 2 !== 0); 43 | console.log(result); 44 | ``` 45 | **Output** 46 | ``` 47 | [1, 3, 5, 7] 48 | ``` 49 | *** 50 | ## reduce() Method 51 | ```js 52 | const numbers = [1, 2, 3, 4, 5]; 53 | 54 | const reducer = (previousValue, currentValue) => previousValue + currentValue; 55 | 56 | const result = numbers.reduce(reducer); 57 | console.log(result); 58 | ``` 59 | **Output** 60 | ``` 61 | 15 62 | ``` 63 | *** 64 | ## Programming Task 65 | **Create an array named greet with values 'hello', 'hi' and 'welcome'. 66 | const greet = ['hello', 'hi', 'welcome']; 67 | Use map() method to add a new string 'world' to each element of the array. 68 | Remember two words should be separated by a space. 69 | From the new array filter out the element that has more than 6 characters.** 70 | ### Solution: 71 | ```js 72 | const greet = ['hello', 'hi', 'welcome']; 73 | const newValue = greet.map(item => item + ' world'); 74 | console.log(newValue); 75 | const finalValue = newValue.filter(item => item.length > 6); 76 | console.log(finalValue); 77 | ``` 78 | **Output** 79 | ``` 80 | ["hello world", "hi world", "welcome world"] 81 | ["hello world", "hi world", "welcome world"] 82 | ``` 83 | *** 84 | ## Programiz Quiz 85 | **Q. What is the output of the program?** 86 | ```js 87 | const numbers = [53, 22, 0, 10, 15]; 88 | 89 | const reducer = (previousValue, currentValue) => previousValue - currentValue; 90 | let result = array.reduce(reducer); 91 | console.log(result); 92 | ``` 93 | 1. Error 94 | 2. 0 95 | 3. 6 96 | 4. 10 97 | 98 | Answer: 3 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /23. for...in and for...of Loop.md: -------------------------------------------------------------------------------- 1 | # for...in and for...of Loop 2 | ## JavaScript for...of loop 3 | **Example 1** 4 | ```js 5 | const numbers = [1, 3, 5, 9]; 6 | 7 | for (const element of numbers) { 8 | console.log(element); 9 | } 10 | ``` 11 | **Output** 12 | ``` 13 | 1 14 | 3 15 | 5 16 | 9 17 | ``` 18 | *** 19 | **Example 2** 20 | ```js 21 | const members = ['John', 'Sara', 'Jack']; 22 | 23 | for (const name of members) { 24 | let fullname = name + ' ' + 'Smith'; 25 | console.log(fullname); 26 | } 27 | ``` 28 | **Output** 29 | ``` 30 | John Smith 31 | Sara Smith 32 | Jack Smith 33 | ``` 34 | ## for...of with strings 35 | ```js 36 | const name = 'Programiz'; 37 | 38 | for (let i of name) { 39 | console.log(i); 40 | } 41 | ``` 42 | **Output** 43 | ``` 44 | P 45 | r 46 | o 47 | g 48 | r 49 | a 50 | m 51 | i 52 | z 53 | ``` 54 | *** 55 | ## JavaScript for...in loop 56 | **Example 1** 57 | ```js 58 | const student = { 59 | name: 'Monica', 60 | class: 7, 61 | age: 12 62 | }; 63 | 64 | console.log(student); 65 | ``` 66 | **Output** 67 | ``` 68 | { name: 'Monica', class: 7, age: 12 } 69 | ``` 70 | *** 71 | **Example 2** 72 | ```js 73 | const student = { 74 | name: 'Monica', 75 | class: 7, 76 | age: 12 77 | } 78 | 79 | for (let key in student) { 80 | console.log(key); 81 | } 82 | ``` 83 | **Output** 84 | ``` 85 | name 86 | class 87 | age 88 | ``` 89 | **Example 3** 90 | ```js 91 | const student = { 92 | name: 'Monica', 93 | class: 7, 94 | age: 12 95 | } 96 | 97 | for (let key in student) { 98 | let value = student[key]; 99 | console.log(`${key} => ${value}`); 100 | } 101 | ``` 102 | **Output** 103 | ``` 104 | name => Monica 105 | class => 7 106 | age => 12 107 | ``` 108 | ## Change Object values using for...in 109 | ```js 110 | const employee = { 111 | Jack: 24000, 112 | Paul: 34000, 113 | Monica: 32000 114 | } 115 | 116 | for (let i in employee) { 117 | let salary = employee[i] + 5000; 118 | console.log(`${i} : ${salary}`); 119 | } 120 | ``` 121 | **Output** 122 | ``` 123 | Jack : 29000 124 | Paul : 39000 125 | Monica : 37000 126 | ``` 127 | ## Programming Task 128 | **Create an array named names with values: "jack", "rosie", "mira". 129 | Now, use a for...of loop to print each name of the array. 130 | However, while printing the name, change the first character to uppercase.** 131 | ### Solution: 132 | ```js 133 | const names = ['jack', 'rosie', 'mira']; 134 | for (let element of names) { 135 | const result = element.charAt(0).toUpperCase() + element.slice(1); 136 | console.log(result); 137 | } 138 | ``` 139 | **Output** 140 | ``` 141 | "Jack" 142 | "Rosie" 143 | "Mira" 144 | ``` 145 | *** 146 | ## Programiz Quiz 147 | **Q. What is the output of the program?** 148 | ```js 149 | const student = { 150 | name: 'Monica', 151 | class: 7, 152 | age: 12 153 | } 154 | 155 | for ( let key of student ) { 156 | console.log(`${key} => ${student[key]}`); 157 | } 158 | ``` 159 | 1. Error 160 | 2. name => Monica 161 | class => 7 162 | age => 12 163 | 3. name => name 164 | class => class 165 | age => age 166 | 4. Monica => Monica 167 | 7 => 7 168 | 12 => 12 169 | 170 | Answer: 3 171 | 172 | -------------------------------------------------------------------------------- /24. Numbers.md: -------------------------------------------------------------------------------- 1 | # Numbers 2 | ```js 3 | const a = 3; 4 | const b = 3.13; 5 | 6 | console.log(typeof(a)); 7 | console.log(typeof(b)); 8 | ``` 9 | **Output** 10 | ``` 11 | number 12 | number 13 | ``` 14 | *** 15 | ## JavaScript Number() Function 16 | ```js 17 | const str = '23'; 18 | console.log(typeof(str)); 19 | 20 | const num = Number(str); 21 | console.log(typeof(num)); 22 | ``` 23 | **Output** 24 | ``` 25 | string 26 | number 27 | ``` 28 | *** 29 | ```js 30 | const bool = true; 31 | const result = Number(bool); 32 | console.log(result); 33 | ``` 34 | **Output** 35 | ``` 36 | 1 37 | ``` 38 | *** 39 | ## JavaScript NaN 40 | ```js 41 | const str = 'hello'; 42 | const result = Number(str); 43 | console.log(result); 44 | ``` 45 | **Output** 46 | ``` 47 | NaN 48 | ``` 49 | *** 50 | ```js 51 | const result = 4 - 'hello'; 52 | console.log(result); 53 | ``` 54 | **Output** 55 | ``` 56 | NaN 57 | ``` 58 | *** 59 | ```js 60 | const result = isNaN((9); 61 | console.log(result); 62 | ``` 63 | **Output** 64 | ``` 65 | false 66 | ``` 67 | *** 68 | ```js 69 | const result = isNaN('hello'); 70 | console.log(result); 71 | ``` 72 | **Output** 73 | ``` 74 | true 75 | ``` 76 | *** 77 | ## JavaScript Number Methods 78 | ### 1. isInteger() Method 79 | ```js 80 | const num = 32; 81 | 82 | const result = Number.isInteger(num); 83 | console.log(result); 84 | ``` 85 | **Output** 86 | ``` 87 | true 88 | ``` 89 | *** 90 | ```js 91 | const num = 32.56; 92 | 93 | const result = Number.isInteger(num); 94 | console.log(result); 95 | ``` 96 | **Output** 97 | ``` 98 | false 99 | ``` 100 | *** 101 | ### 2. parseInt() and parseFloat() Method 102 | ```js 103 | const num = '5'; 104 | const result = Number.parseInt(num); 105 | console.log(result); 106 | console.log(Number.isInteger(result)); 107 | ``` 108 | **Output** 109 | ``` 110 | 5 111 | true 112 | ``` 113 | *** 114 | ```js 115 | const num = 5.234234324324; 116 | const result = Number.parseInt(num); 117 | console.log(result); 118 | console.log(Number.isInteger(result)); 119 | ``` 120 | **Output** 121 | ``` 122 | 5 123 | true 124 | ``` 125 | *** 126 | ### 3. parseFloat() Method 127 | ```js 128 | const num = '5.23423423'; 129 | 130 | const result = Number.parseFloat(num); 131 | console.log(result); 132 | console.log(typeof(result)); 133 | ``` 134 | **Output** 135 | ``` 136 | 5.23423423 137 | number 138 | ``` 139 | *** 140 | ### 3. toFixed() Method 141 | ```js 142 | const num = 5.234234324324; 143 | const result = num.toFixed(2); 144 | console.log(result); 145 | ``` 146 | **Output** 147 | ``` 148 | 5.23 149 | ``` 150 | *** 151 | ## Programming Task 152 | **Take numeric string input from the user. 153 | Convert that input into numbers. 154 | If the input has been converted to numbers, multiply that number by 10 and display the output. 155 | Else it is not converted to numbers, display "The input is not a numeric string."** 156 | ### Solution: 157 | ```js 158 | const numberInput = prompt("Enter a number: "); 159 | const result = Number(numberInput); 160 | if(typeof(result) == "number") { 161 | console.log(result * 10); 162 | } else { 163 | console.log("The input is not a numeric string."); 164 | } 165 | ``` 166 | **Output** 167 | ``` 168 | Enter a number: 5.5 169 | 55 170 | ``` 171 | *** 172 | ## Programiz Quiz 173 | **Q. What is the output of the program?** 174 | ```js 175 | const num = '23' - '55'; 176 | console.log(num); 177 | ``` 178 | 1. Error 179 | 2. NaN 180 | 3. 32 181 | 4. -32 182 | 183 | Answer: 4 184 | -------------------------------------------------------------------------------- /3. JavaScript Take Input.md: -------------------------------------------------------------------------------- 1 | # 3. JavaScript Take Input 2 | 3 | ## Take Input in JavaScript 4 | ```js 5 | const name = prompt("Enter your name:"); 6 | console.log(`Name: ${name}`); 7 | ``` 8 | 9 | **Output** 10 | ``` 11 | Enter your name: Punit 12 | Name: Punit 13 | ``` 14 | ## JavaScript number input 15 | ```js 16 | const age = prompt("Enter your age:"); 17 | console.log(age); 18 | ``` 19 | **Output** 20 | ``` 21 | Enter your age: 28 22 | 28 23 | ``` 24 | ## Find the type 25 | ```js 26 | const age = prompt("Enter your age"); 27 | console.log(age); 28 | console.log(typeof(age)); 29 | ``` 30 | **Output** 31 | ``` 32 | Enter your age : 28 33 | Hello, Your age is 28 34 | "string" 35 | ``` 36 | *** 37 | ```js 38 | const intValue = 5; 39 | console.log(typeof(intValue)); 40 | 41 | const floatValue = 5.5; 42 | console.log(typeof(floatValue)); 43 | ``` 44 | **Output** 45 | ``` 46 | "number" 47 | "number" 48 | ``` 49 | *** 50 | ```js 51 | const age = parseInt(prompt("Enter your age:")); 52 | console.log(age); 53 | console.log(typeof(age)); 54 | ``` 55 | **Output** 56 | ``` 57 | Enter your age: 28 58 | "number" 59 | ``` 60 | ## Float Input 61 | ```js 62 | const height = parseFloat(prompt('Enter your height ')); 63 | console.log(height); 64 | console.log(typeof(height)); 65 | ``` 66 | **Output** 67 | ``` 68 | Enter a number: 5.65 69 | 5.65 70 | "number" 71 | ``` 72 | ## Error When Using paresInt() and parseFloat() 73 | ```js 74 | let name = parseInt(prompt('Enter your name: ')); 75 | console.log(name); 76 | ``` 77 | **Output** 78 | ``` 79 | Enter your name: Punit 80 | NaN 81 | ``` 82 | *** 83 | ## Programiz Quiz 84 | **What is the type of the ‘number’ variable?** 85 | ```js 86 | const number = prompt(‘Enter a random number’); 87 | console.log(typeof(number)); 88 | ``` 89 | 90 | 1. number 91 | 2. string 92 | 3. NaN 93 | 4. undefined 94 | 95 | Answer: 2 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /4. JavaScript Comments.md: -------------------------------------------------------------------------------- 1 | # 4. JavaScript Comments 2 | 3 | ## Write Comment 4 | ```js 5 | // program to take the user's name 6 | let name = prompt("Enter your name: "); 7 | console.log("Name: " + name); 8 | ``` 9 | ## 10 | **Output** 11 | ``` 12 | Enter your name: James 13 | Name: James 14 | ``` 15 | ## Another comment example 16 | ```js 17 | // program to take the user's name 18 | let name = prompt("Enter your name: "); // take input 19 | console.log("Name: " + name); // print the name variable 20 | ``` 21 | **Output** 22 | ``` 23 | Enter your name: James 24 | Name: James 25 | ``` 26 | ## Prevent Executing Code Using Comments 27 | ```js 28 | // program to take user's name and age 29 | let name = prompt("Enter a name: "); 30 | // let age = prompt("Enter age: "); 31 | console.log("name: " + name); 32 | // console.log("age:", age); 33 | ``` 34 | **Output** 35 | ``` 36 | Enter name: Sarah 37 | name: Sarah 38 | ``` 39 | *** 40 | ## JavaScript Multiline comments 41 | ```js 42 | /*This program is used to take name of the user 43 | You can get the name from the user 44 | and display the result*/ 45 | 46 | let name = prompt("Enter your name:"); 47 | console.log("Name " + name); 48 | ``` 49 | **Output** 50 | ``` 51 | Enter your name: Sarah 52 | Name Sarah 53 | ``` 54 | *** 55 | ## Programmiz Quiz 56 | **Which of the following operators is used as a comment in JavaScript?** 57 | ``` 58 | 1. // 59 | 2.