0% found this document useful (0 votes)
8 views5 pages

JavaScript - 4 (Arrays)

The document provides an overview of arrays in JavaScript, explaining their definition, common methods such as push, pop, unshift, and shift, and how to loop through them using various techniques. It includes examples for each method and loop type, as well as practice tasks to reinforce learning. Additionally, it lists concise JavaScript function tasks to further develop programming skills.

Uploaded by

Ganesh Official
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

JavaScript - 4 (Arrays)

The document provides an overview of arrays in JavaScript, explaining their definition, common methods such as push, pop, unshift, and shift, and how to loop through them using various techniques. It includes examples for each method and loop type, as well as practice tasks to reinforce learning. Additionally, it lists concise JavaScript function tasks to further develop programming skills.

Uploaded by

Ganesh Official
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arrays – Basics

1. What is an Array?
An array is a collection of values stored in a single variable.
Each value has an index (starting from 0).

Example:
let fruits = ["apple", "banana", "mango"];
[Link](fruits[0]); // apple
[Link](fruits[2]); // mango

2. Common Array Methods


push()
Adds an element to the end of the array.

let colors = ["red", "green"];


[Link]("blue");
[Link](colors); // ["red", "green", "blue"]

pop()
Removes the last element from the array.

let animals = ["dog", "cat", "cow"];


[Link]();
[Link](animals); // ["dog", "cat"]

unshift()
Adds an element to the beginning of the array.

let numbers = [2, 3, 4];


[Link](1);
[Link](numbers); // [1, 2, 3, 4]
shift()

Removes the first element of the array.

let cities = ["Delhi", "Mumbai", "Chennai"];


[Link]();
[Link](cities); // ["Mumbai", "Chennai"]

3. Looping through Arrays


You can loop through arrays using different loops.

for Loop
Runs from index 0 to array length – 1.

let fruits = ["apple", "banana", "grapes"];

for (let i = 0; i < [Link]; i++) {


[Link](fruits[i]);
}

Output:

apple
banana
grapes

for...of Loop
Used to directly access each item (simpler syntax).

let fruits = ["apple", "banana", "grapes"];

for (let fruit of fruits) {


[Link](fruit);
}

forEach() Method
Runs a function for each element.

let fruits = ["apple", "banana", "grapes"];

[Link](function(fruit) {
[Link](fruit);
});

4. Quick Summary
Method Action Example Result

push() Add to end [Link]("x") adds last

pop() Remove from end [Link]() removes last

unshift Add to start [Link](" adds first


() x")

shift() Remove from start [Link]() removes first

5. Practice Tasks
1. Create an array of 5 student names. Add one more using push().

2. Remove the first element using shift() and print the new array.

3. Add an element at the beginning using unshift().

4. Use a for loop to print each name.

5. Use forEach() to print "Hello, [name]" for each student.

Array & Loop – Practice Questions

1. Create an array of 3 fruits and add one more using push().

2. Remove the last element from an array using pop().

3. Add one element at the start using unshift().

4. Remove the first element using shift().


5. Print all elements of an array using a for loop.

6. Print all elements using for...of loop.

7. Use forEach() to print each item.

8. Find the sum of all numbers in an array.

9. Print only even numbers from [2, 5, 8, 11, 14].

10. Create an array and print its length.

11. Add two elements, then remove one and print the array.

12. Print each array element with its index.

13. Double each number in [1, 2, 3, 4] using a loop.

14. Check if a name "John" exists in an array.

15. Print array elements in reverse order.

16. Create an empty array and add 3 values using push().

17. Use a loop to print all student names from an array.

18. Use shift() and unshift() together and print result.

19. Print the first and last element of an array.

20. Create an array of 5 numbers and print only numbers > 10.

JavaScript Function Tasks (Concise)


1. Create a function that prints "Hello JavaScript".

2. Create a function that takes a name and prints "Hello, [name]".

3. Write a function to add two numbers and print the result.

4. Write a function that returns the square of a number.


5. Create a function that checks if a number is even or odd.

6. Write a function to find the largest of two numbers.

7. Write a function that multiplies three numbers and returns the result.

8. Create a function to calculate the area of a rectangle.

9. Write a function that returns the sum of all elements in an array.

10. Create a function that counts how many even numbers are in an array.

11. Write a function that converts Celsius to Fahrenheit.

12. Create a function that returns the reverse of a string.

13. Write an arrow function to print the cube of a number.

14. Create a function expression that adds 10 to any number.

15. Write a function that prints all items in an array.

16. Create a function that returns "Positive" or "Negative" for a given number.

17. Write a function that checks if a given string contains the word "JavaScript".

18. Create a function that takes age and prints "Adult" if age ≥ 18.

19. Write a function with default parameters (e.g., greeting message).

20. Create a function that returns the factorial of a number.

You might also like