Computer Science, asked by rakhirawat1981, 5 months ago

explain the function and array in JavaScript with example 50 to 80 words

Answers

Answered by payalft029
1

JavaScript Basic Array Methods

It’s recommended to go through Arrays in JavaScript . We would be discussing the following array function:

Array.push() : Adding Element at the end of an Array. As array in JavaScript are mutable object, we can easily add or remove elements from the Array. And it dynamically changes as we modify the elements from the array.

Syntax:

Array.push(item1, item2 …)

Parameters: Items to be added to an array.

Description: This method is used add elements at the end of an array.

// Adding elements at the end of an array

// Declaring and initializing arrays

var number_arr = [ 10, 20, 30, 40, 50 ];

var string_arr = [ "piyush", "gourav", "smruti", "ritu" ];

// push()

// number_arr contains [10, 20, 30, 40, 50, 60]

number_arr.push(60);

// We can pass multiple parameters to the push()

// number_arr contains

// [10, 20, 30, 40, 50, 60, 70, 80, 90]

number_arr.push(70, 80, 90);

// string_arr contains

// ["piyush", "gourav", "smruti", "ritu", "sumit", "amit"];

string_arr.push("sumit", "amit");

// Printing both the array after performing push operation

console.log("After push op " + number_arr);

console.log("After push op " + string_arr);

Array.unshift() : Adding elements at the front of an Array

Syntax:

Array.unshift(item1, item2 …)

Parameters: Items to be added to the array

Description: This method is used to add elements at the beginning of an array.

// Adding element at the beginning of an array

// Declaring and initializing arrays

var number_arr = [ 20, 30, 40 ];

var string_arr = [ "amit", "sumit" ];

// unshift()

// number_arr contains

// [10, 20, 20, 30, 40]

number_arr.unshift(10, 20);

// string_arr contains

// ["sunil", "anil", "amit", "sumit"]

string_arr.unshift("sunil", "anil");

// Printing both the array after performing unshift operation

console.log("After unshift op " + number_arr);

console.log("After unshift op " +

Answered by BrainlyFlash
3

{\huge{\star{\underbrace{\tt{\red{Answer}}}}}}{\huge{\star}}

{\mathcal{\gray{FUNCTION \ :-}}}

function are the modulus from which Java programs are built. Like a class , a function or method has two parts : function declaration and function body. The function declaration defines all the function's attributes, such as access levels, return types , names and arguments. The body contains the executable portion.

Methods or functions implement the behaviour of the objects.

Methods exist inside a class. A Java program can have many classes and each class can have several methods, but one class in every program contains one main () method.

Example -

public int Add (int a, int b)

public float Add (float a, float b)

{\mathcal{\gray{ARRAY \ :-}}}

An array is a group of similar data types that are referenced by a common name. Arrays can be of one or more dimensions. An array is a way of grouping related information as it is a structure that can hold multiple values of the same type.

Example -

int amount [ ] = new int [10];

int [ ] amount = new int [10];

String name [ ] = new String [100];

int num [ ] = {100,200,....};

 \\

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

\Large\mathcal{\fcolorbox{lime}{black}{\red{Hope it's help  ⚓⚓}}}

\large\mathcal{\fcolorbox{yellow}{teal}{\orange{Please mark me as brainliest (. ❛ ᴗ ❛.)}}}

Similar questions