Array and Object

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Non primitive data types in

javascript
Syed Muhammad Jawad
SE120212042
Table of contents

01 02
Array Object
Different method of Array Different method of object
01
Array
Array

An array is a collection of similar data elements stored at contiguous memory locations. It is the
simplest data structure where each data element can be accessed directly by only using its index
number.
Arrays in JavaScript
JavaScript arrays are collections of items. These items can be of the same
type or different but arrays provide a way to group them together into acommon collection.

Array values are indexed which means in order to obtain any value from this collection, a
developer should use its index (starts at 0) within the collection itself. Let’s see a visual
representation of an array.
Example
Without using Array

Using Array
Array and loop
One of the most common uses of loops in programming is to iterate over an array's
elements and perform some action on each element. By iterate, we mean that we
process each element one at a time, in sequence from the first to the last element.

Example
Array Methods

• Pop () Method
• Push () Method
• Shift () Method
• Unshift () Method
• Length () Method
• ToString() Method
• Delete() Method
• Concat() Method
• Splice() Method
• Slice() Method
Pop () Method
• Removes the end element of JavaScript Array , the Pop() method is used.
• The pop () method return the value that was popped out

Example
Push() Method
• Adding new elements to JavaScript Array , We have used the Javascript in-
built push() method

• The push() method returns the new array length

Example
Shift () Method
• The shift() method removes the first array element and "shifts" all other
elements to a lower index.

• The shift() method returns the value that was


"shifted out"

Example
Unshift () Method
• The unshift() method adds a new element to an array (at the beginning), and
"unshifts" older elements

• The unshift() method returns the new array length

Example
Length () Method
• The length property returns the length (size) of an array

Example
ToString() Method
• The JavaScript method toString() converts an array to a string of (comma
separated) array values

Example
Delete() Method

• Array elements can be deleted using the JavaScript operator delete.


• Using delete leaves undefined holes in the array.

Example
Concat() Method
• The concat() method creates a new array by merging (concatenating) existing arrays:

• The concat() method does not change the existing arrays. It always returns a new array

• The concat() method can also take strings as arguments


Example
Concatenating with Rest operator
Rest operator
The rest operator is used to put the rest of some specific user-supplied values
into a JavaScript array. Represented with three dots (...).

Example
Splice() Method
• The splice() method can be used to add new items to an array

• The first parameter (2) defines the position where new elements should
be added (spliced in).
• The second parameter (0) defines how many elements should be removed.

Example 1
Example 2
• The first parameter (1) defines the position where new elements should
be added (spliced in).
• The second parameter (3) defines how many elements should
be removed.
Slice() Method
• The slice() method slices out a piece of an array into a new array.
• The slice() method creates a new array.
• The slice() method does not remove any elements from the source array.

• The slice() method can take two arguments like slice(1, 3).
• The method then selects elements from the start argument, and up to (but not
including) the end argument.
Example
2
Object
Objects in JavaScrip
• JavaScript is designed on a simple object-based paradigm. An object is a collection
of properties and a property is an association between a name (or key) and a value.
A property's value can be a function, in which case the property is known as a
method.

• A cup is an object, with properties. A cup has a color, a design, weight, a material it is
made of, etc. The same way, JavaScript objects can have properties, which define
their characteristics.
Example
Dot Notation
• Dot notation is used to access object properties using a dot followed by
the property name.
Bracket notation
• Bracket notation is used to access object properties using the object name inside a set
of square brackets [] followed by the property name.
Differences between Dot Notation and Bracket
Notation
• One key difference between the two is that dot notation can only be used to access
properties that have valid identifier names, while bracket notation can be used to
access properties using any string, including variables and expressions.
• Additionally, bracket notation is useful when the property name is not known until
runtime.
• In general, dot notation is more readable and easier to understand, but bracket notation
can be more flexible.
Example
Object Method
• Function inside Object is kown as Method.
THANKS
!

You might also like