Join Regular Classroom : Visit ClassroomTech

JavaScript – codewindow.in

Related Topics

HTML

Introduction
Html page 1
Html page 2
Html page3
Html page4

HTML Elements and structure
Html page 5
Html page 6
Html page 7

HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10

HTML Lists and Tables
Html page 11
Html page 12
Html page 13

HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16

HTML Images and Media
Html page 17
Html page 18

HTML Links and Anchors
Html page 19
Html page 20
Html page 21

HTML Styles and Formatting
Html page 22

HTML Semantic Elements
Html page 23
Html page 24

HTML Attributes
Html page 25
Html page 26

HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30

HTML Document and Browser Support
Html page 31
Html page 32

HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36

HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39

HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42

JAVASCRIPT

let i = 0;
while (i < 10) {
  if (i === 5) {
    break;
  }
  console.log(i);
  i++;
}
This code will print the numbers from 0 to 4 to the console. The break statement will be executed when the value of i reaches 5, and the loop will terminate.
Here is an example of how to use the continue statement in a loop:
let i = 0;
while (i < 10) {
  if (i === 5) {
    // skip the current iteration
    continue;
  }
  console.log(i);
  i++;
}
This code will print the numbers from 0 to 4, and then 6 to 9 to the console. The continue statement will be executed when the value of i reaches 5, and the current iteration of the loop will be skipped. The next iteration of the loop will start with the value of i being 6.
The break and continue statements are powerful tools that can be used to control the flow of execution in a loop in JavaScript. They can be used to make your code more efficient and easier to read.
const numbers = [1, 2, 3, 4, 5];

for (const number of numbers) {
  console.log(number);
}
This code will log the numbers 1 through 5 to the console. The for...of loop iterates over the elements of the array numbers. The number variable will be set to each element of the array in turn. The code inside the loop will be executed once for each element of the array.
Here is an example of a for...in loop:
const object = {
  name: "John Doe",
  age: 30,
  address: "123 Main Street",
};

for (const property in object) {
  console.log(property);
}
This code will log the following to the console:
name
age
address
The for...in loop iterates over the properties of the object object. The property variable will be set to the name of each property in the object, in alphabetical order. The code inside the loop will be executed once for each property in the object.
The for...of loop is a newer addition to JavaScript, and it is generally considered to be the more efficient of the two loops. The for...in loop is more flexible, as it can be used to iterate over any object, even objects that do not have a well-defined iteration order.
However, the for...in loop can be slower than the for...of loop, as it has to iterate over the properties of the object in order to get the elements. The for...of loop can only iterate over iterable objects, such as arrays and strings.
The best loop to use depends on the specific task that you need to perform. If you are iterating over an iterable object, the for...of loop is usually the best choice. If you are iterating over an object with properties, the for...in loop may be a better choice.
const numbers = [1, 2, 3, 4, 5];
const letters = ["a", "b", "c", "d", "e"];

for (const number of numbers) {
  for (const letter of letters) {
    console.log(`${number} ${letter}`);
  }
}
This code will log the numbers and letters in a grid pattern to the console. The outer loop iterates over the elements of the array numbers. The inner loop iterates over the elements of the array letters. The code inside the inner loop will be executed once for each element of the array letters, and the value of the variable number will be passed to the code inside the inner loop.
Here is another example of a nested loop
const numbers = [1, 2, 3, 4, 5];
const letters = ["a", "b", "c", "d", "e"];

for (const number of numbers) {
  for (let i = 0; i < letters.length; i++) {
    console.log(`${number} ${letters[i]}`);
  }
}
This code is similar to the previous example, but it uses a for loop instead of a for...in loop to iterate over the array letters. The for loop allows us to control the number of times the inner loop is executed.
Nested loops can be used to iterate over two or more collections of data at the same time. They can be used to perform complex calculations or to generate complex output.
It is important to note that nested loops can be slow, so they should be used sparingly. If you are using nested loops, it is important to make sure that they are as efficient as possible.
array.forEach(callbackFunction);
The callback function takes two arguments: the current element of the array and the index of the current element. The callback function can be used to perform any operation on the current element of the array.
Here is an example of how to use the Array.forEach() method:
const numbers = [1, 2, 3, 4, 5];

numbers.forEach((number, index) => {
  console.log(`The number at index ${index} is ${number}`);
});
This code will log the following to the console:
The number at index 0 is 1
The number at index 1 is 2
The number at index 2 is 3
The number at index 3 is 4
The number at index 4 is 5
The callback function in this example takes two arguments: the current element of the array, which is the number number, and the index of the current element, which is the number index. The callback function logs the number at the specified index to the console.
The Array.forEach() method is a powerful tool that can be used to iterate over the elements of an array in JavaScript. It is a concise and easy-to-use method, and it can be used to perform any operation on the elements of the array.
const numbers = [1, 2, 3, 4, 5];

const doubledNumbers = numbers.map(number => number * 2);
console.log(doubledNumbers); // [2, 4, 6, 8, 10]

const sumOfNumbers = numbers.reduce((accumulator, number) => accumulator + number, 0);
console.log(sumOfNumbers); // 15

const evenNumbers = numbers.filter(number => number % 2 === 0);
console.log(evenNumbers); // [2, 4, 6]
The map method in this example takes a callback function that doubles the value of each element in the array. The reduce method in this example takes a callback function that adds the current element to the accumulator. The filter method in this example takes a callback function that returns true for even numbers.
The map, reduce, and filter array methods are all powerful tools that can be used to manipulate arrays in JavaScript. They are concise and easy-to-use methods, and they can be used to perform a variety of tasks on arrays.

      

Popular Category

Topics for You

HTML

Introduction
Html page 1
Html page 2
Html page3
Html page4

HTML Elements and structure
Html page 5
Html page 6
Html page 7

HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10

HTML Lists and Tables
Html page 11
Html page 12
Html page 13

HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16

HTML Images and Media
Html page 17
Html page 18

HTML Links and Anchors
Html page 19
Html page 20
Html page 21

HTML Styles and Formatting
Html page 22

HTML Semantic Elements
Html page 23
Html page 24

HTML Attributes
Html page 25
Html page 26

HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30

HTML Document and Browser Support
Html page 31
Html page 32

HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36

HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39

HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42

Go through our study material. Your Job is awaiting.

Recent Posts
Categories