Maharashtra's Most Trusted

with more than 0 members

Chapter 2 - Array, Function & String

For MSBTE Diploma CO / IT / AIML Branch

Define Array -: 

In JavaScript, an array is a special type of object used to store multiple values in a single variable. Arrays are ordered collections of items (elements), where each element is associated with an index, starting from 0.

You can define an array in JavaScript using one of the following methods:

1. Using square brackets [] (Array Literal)

 

2. Using the Array constructor


Properties of Arrays:

  • Arrays can contain elements of different data types, including numbers, strings, objects, or even other arrays.
  • Arrays are dynamic, meaning you can change their size at runtime.

Adding Element in Array -: 

In JavaScript, you can add elements to an array in multiple ways. The two most common methods are using the push() method and assigning a value to a specific index in the array. Let’s go over each method with examples:

1. Using push() Method

The push() method adds one or more elements to the end of an array.

Example:


2. Using Index to Assign a New Element

You can directly add an element by assigning it to a specific index. If the index already exists, it replaces the element at that index. If the index doesn’t exist, it creates a new one.

Example:


Sorting the array -: 

In JavaScript, the sort() method is used to sort the elements of an array in place (meaning the original array is modified) and returns the sorted array. By default, the sort() method sorts elements as strings in lexicographical (dictionary) order. This can lead to unexpected results when sorting numbers.

Let’s go over how to use the sort() method, both with and without a custom comparison function.

1. Sorting an Array of Strings (Default Behavior)

When sorting an array of strings, the default behavior works as expected.

Example:


2. Sorting an Array of Numbers (Default Behavior)

When sorting numbers, the default behavior may not work as you expect because sort() treats the elements as strings. This can lead to incorrect results when sorting numbers.

Example (incorrect sorting for numbers):


Join() & Concat() in Java Script -: 

In JavaScript, both the join() and concat() methods are used with arrays, but they serve different purposes. Let’s explore how each method works, with examples.

1. join() Method

The join() method creates and returns a string by concatenating all the elements of an array, separated by a specified delimiter. If no delimiter is provided, the elements are joined by commas by default.

Syntax:

 

Example 1: Default Behavior

Example 2: Using a Custom Separator

 

2. concat() Method

The concat() method is used to merge two or more arrays. It creates a new array by combining the elements of the existing arrays, without modifying the original arrays.

Syntax:

 

Example 1: Concatenating Two Arrays

Push() & Pop() in Java Script Array -:

In JavaScript, the push() and pop() methods are used to manipulate arrays by adding or removing elements from the end of the array. These methods are essential for working with arrays as they allow dynamic changes to the array length and content.

1. push() Method

The push() method adds one or more elements to the end of an array and returns the new length of the array.

Syntax:

 

Example 1: Adding a Single Element


Example 2: Adding Multiple Elements

 

2. pop() Method

The pop() method removes the last element from an array and returns the removed element. This method changes the length of the array by reducing it by 1.

Syntax:

 

Example 1: Removing the Last Element

end of part 1 -------->

Explore Other Chapters