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 numbers = [1, 2, 3, 4, 5];
let length = numbers.length;

console.log(length); // Output: 5
In this example, numbers.length returns the length of the numbers array, which is 5.
It’s important to note that the length property represents the number of elements in the array, not the maximum index. The length is always one greater than the highest index in the array, as array indices start from 0.
If you modify the array by adding or removing elements, the length property automatically adjusts accordingly:
let fruits = ['apple', 'banana', 'cherry'];
console.log(fruits.length); // Output: 3

fruits.push('date');
console.log(fruits.length); // Output: 4

fruits.pop();
console.log(fruits.length); // Output: 3
In this example, the length property reflects the changes made to the fruits array.
// Converting an array-like object to an array using Array.from()
let arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3 };
let array = Array.from(arrayLike);
console.log(array); // Output: ['a', 'b', 'c']
While array-like objects share some similarities with arrays, it’s important to note their differences in terms of available methods, prototype inheritance, and mutability.
let queue = [];

// Enqueue elements
queue.push('element1');
queue.push('element2');
queue.push('element3');

console.log(queue); // Output: ['element1', 'element2', 'element3']
In this example, push() adds elements to the end of the queue array.
2. Dequeue: To remove elements from the front of the queue, you can use the shift() method.
let queue = ['element1', 'element2', 'element3'];

// Dequeue an element
let dequeuedElement = queue.shift();

console.log(dequeuedElement); // Output: 'element1'
console.log(queue);          // Output: ['element2', 'element3']
The shift() method removes and returns the first element from the queue array, simulating the dequeue operation.
3. Peek: To get the element at the front of the queue without removing it, you can access the element at index 0.
let queue = ['element1', 'element2', 'element3'];

// Get the element at the front of the queue
let frontElement = queue[0];

console.log(frontElement); // Output: 'element1'
console.log(queue);       // Output: ['element1', 'element2', 'element3']
In this example, queue[0] retrieves the first element of the queue array without removing it.
By using these array methods in the appropriate sequence, you can effectively utilize an array as a queue data structure in JavaScript.
let stack = [];

// Push elements onto the stack
stack.push('element1');
stack.push('element2');
stack.push('element3');

console.log(stack); // Output: ['element1', 'element2', 'element3']
In this example, push() adds elements to the end of the stack array, effectively simulating the push operation in a stack.
2. Pop: To remove elements from the top of the stack, you can use the pop() method.
let stack = ['element1', 'element2', 'element3'];

// Pop an element from the stack
let poppedElement = stack.pop();

console.log(poppedElement); // Output: 'element3'
console.log(stack);        // Output: ['element1', 'element2']
The pop() method removes and returns the last element from the stack array, simulating the pop operation in a stack.
3.Peek: To get the element at the top of the stack without removing it, you can access the last element using the index -1.
let stack = ['element1', 'element2', 'element3'];

// Get the element at the top of the stack
let topElement = stack[stack.length - 1];

console.log(topElement); // Output: 'element3'
console.log(stack);     // Output: ['element1', 'element2', 'element3']
In this example, stack[stack.length - 1] retrieves the last element of the stack array without removing it.
By utilizing these array methods in the appropriate order, you can effectively use an array as a stack data structure in JavaScript.

      

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