0% found this document useful (0 votes)
2 views

5. Javascript Objects Part 2

The document provides an overview of JavaScript arrays, including their structure and various built-in methods for manipulation such as push, pop, and slice. It also discusses iteration techniques using loops like for, forEach, and for...of, as well as methods for array manipulation like map, filter, and reduce. Additionally, it covers JavaScript's Date, Math, Number, and Boolean objects, detailing their functionalities and usage.

Uploaded by

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

5. Javascript Objects Part 2

The document provides an overview of JavaScript arrays, including their structure and various built-in methods for manipulation such as push, pop, and slice. It also discusses iteration techniques using loops like for, forEach, and for...of, as well as methods for array manipulation like map, filter, and reduce. Additionally, it covers JavaScript's Date, Math, Number, and Boolean objects, detailing their functionalities and usage.

Uploaded by

sheikhoo1274
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

www.tuf.edu.

pk
unioffaisalabad

Web Design and Development


JAVASCRIPT OBJECTS

Aasma Khalid
Lecturer
The University of Faisalabad
Arrays and Array Methods www.tuf.edu.pk
unioffaisalabad

• One of the most important data structures in JavaScript is the


array, which is a collection of elements. In this blog post, we
will explore the basics of JavaScript arrays and the various array
methods that can be used to manipulate them.
• An array in JavaScript is a collection of elements enclosed in
square brackets. Elements can be of any data type, including
numbers, strings, and other arrays.
www.tuf.edu.pk
unioffaisalabad

• For example, the following is a valid array in JavaScript:


www.tuf.edu.pk
unioffaisalabad

• JavaScript provides a number of built-in methods for manipulating arrays. Some of the most commonly used
array methods are:
METHODS

length - This method returns the number of elements in an


array.
push - This method is used to add an element to the end of
an array.
pop - This method is used to remove the last element of an
array.
shift - This method is used to remove the first element of an
array.
unshift - This method is used to add an element to the
beginning of an array.
slice - This method is used to extract a portion of an array.

splice - This method is used to add or remove elements


from an array.
Loops with Arrays www.tuf.edu.pk
unioffaisalabad

• One of the most important data structures in JavaScript is the


array, which is a collection of elements. When working with
arrays, it is often necessary to iterate through each element in
the array, which is where loops come in. In this blog post, we
will explore how to use loops with arrays in JavaScript.
• JavaScript provides several ways to iterate through an array,
including the for loop, forEach method, and for...of loop.
www.tuf.edu.pk
unioffaisalabad

• for loop - This is the most basic way to iterate through an array.
The for loop uses a counter variable that is incremented on
each iteration. For example, the following code will print out
each element in the array:
www.tuf.edu.pk
unioffaisalabad

• forEach method - This method is a more concise way to iterate


through an array. The forEach method takes a callback function
as its argument, which is called on each element in the array.
For example, the following code will print out each element in
the array:
www.tuf.edu.pk
unioffaisalabad

• for...of loop - This is a more recent addition to JavaScript, and it


is the most concise way to iterate through an array. It allows
you to iterate through the elements of an array without having
to access the index, and it works with any iterable object, not
just arrays. For example, the following code will print out each
element in the array:
Map, Filter and Reduce www.tuf.edu.pk
unioffaisalabad

• One of the most important data structures in JavaScript is the


array, which is a collection of elements. When working with
arrays, it is often necessary to manipulate the elements in
various ways, which is where the built-in methods map, filter,
and reduce come in. In this blog post, we will explore how to
use these methods to manipulate arrays in JavaScript.

www.tuf.edu.pk
unioffaisalabad

• Map - The map method is used to create a new array with the
result of a callback function called on each element in the
original array. The callback function takes one argument, the
current element, and returns the new value for that element.
For example, the following code will return an array of each
element multiplied by 2:
www.tuf.edu.pk
unioffaisalabad

• Filter - The filter method is used to filter an array based on a


certain condition. The filter method takes a callback function as
its argument, which is called on each element in the array. If
the function returns true, the element is included in the new
filtered array. For example, the following code will return an
array of all even numbers in the original array:
www.tuf.edu.pk
unioffaisalabad

• Reduce - The reduce method is used to reduce an array to a


single value. The reduce method takes a callback function as its
argument, which is called on each element in the array. The
callback function takes two arguments, the accumulator and
the current element, and returns the new value for the
accumulator. For example, the following code will return the
sum of all elements in the array:
Date www.tuf.edu.pk
unioffaisalabad

• JavaScript's Date object allows you to work with dates and times in
your scripts. It can be used to get the current date and time, or to
manipulate and format dates and times in various ways.
• Creating a new Date object is simple. You can either create a new
date object with the current date and time by calling new
Date() with no arguments, or you can create a date object with a
specific date and time by passing in a string or numerical values.
• Here's an example of how you can use the Date object to get the
current date and time:
Math www.tuf.edu.pk
unioffaisalabad

• The JavaScript Math object is a built-in object that provides a


variety of mathematical functions and constants. It can be used
to perform mathematical operations such as trigonometry,
logarithms, and random number generation.
• One of the most commonly used functions in the Math object
is the Math.random() function, which generates a random
number between 0 (inclusive) and 1 (exclusive). This function
can be used to generate random numbers for various purposes
such as games, simulations, and other applications.
Number www.tuf.edu.pk
unioffaisalabad

• The JavaScript Number object is a built-in object that provides a number


of properties and methods for working with numeric values.
The Number object can be used to convert a value to a number, perform
mathematical operations, and format numbers for display.
• One of the most commonly used methods of the Number object is
the Number() function, which can be used to convert a value to a
number. This function can be used to convert strings, booleans, and
other types of values to numbers.
Boolean www.tuf.edu.pk
unioffaisalabad

• The JavaScript Boolean object is a built-in object that provides


a way to work with Boolean (true/false) values in your scripts.
The Boolean object can be used to create Boolean values and
perform logical operations, such as and, or, and not.
• A boolean value can be created by using the Boolean() function
or by assigning a value of true or false to a variable.

You might also like