Assignment Lasr
Assignment Lasr
Date: -04/07/2024
Name: Vyas Harshiv Jigneshkumar
1. What are arrays in JavaScript and how are they used?
In JavaScript, an array is a data structure that stores a
collection of elements, which can be of any data type,
including numbers, strings, objects, and even other
arrays. Arrays are denoted by square brackets [] and are
used to store and manipulate collections of data.
Syntax:
Data_type array name[size];
Example: name[20];
->Declaration: Arrays can be declared using the let or
const keyword, followed by the array name and square
brackets []. For example: let myArray = [];
->Elements: Arrays can hold multiple elements, which
can be added using the push() method or by assigning
values to specific indices. For example: myArray[0] = 5;
->Indexing: Array elements are accessed using their index
(position in the array), starting from 0. For example:
myArray[0] would access the first element.
2. Describe the methods available for manipulating
arrays.
->Manipulating an array means performing various
operations on the array to modify its contents, structure,
or order. Some common examples of array manipulation
include:
Sorting and ordering methods:
- sort() : Sorts the array in place according to a
specified compare function.
- reverse() : Reverses the order of the array in place.
Mutator methods (modify the original array):
- push() : Adds one or more elements to the end of the
array.
- pop() : Removes the last element and returns it.
- shift() : Removes the first element and returns it.
- unshift() : Adds one or more elements to the
beginning of the array.
- splice() : Adds or removes elements at a specified
position.
3. What are objects in JavaScript and how do they differ
from arrays?
-> Arrays are used when we need to collection a list of
elements of the same data type or structure. On the
other hand, objects are used when grouping multiple sets
of data that belong together using labels, where each
property or key has its own value of any type.
Objects differ from arrays in several ways:
1. Structure: Arrays are indexed by numbers, while
objects are indexed by strings (or symbols).
2. Order: Arrays maintain a specific order, while objects
do not have a guaranteed order.
3. Access: Array elements are accessed using numerical
indices (arr[0]), while object properties are accessed
using their keys (obj.name).
4. Flexibility: Objects can have various types of values,
including functions and other objects, while arrays are
primarily used for collections of similar data types.
const person = {
name: 'John',
age: 30,
occupation: 'Developer',
address: {
street: '123 Main St',
city: 'Anytown',
state: 'CA'
}
};
4. Explain the concept of prototype in JavaScript.
in JavaScript, a prototype is an object that is used as a
template for creating new objects. Every object in
JavaScript has a prototype, which is a reference to
another object that contains shared properties and
methods. When a object is created, it inherits the
properties and methods from its prototype, and can also
have its own unique properties and methods.