51-54 - Javascript Functions and Arrays
51-54 - Javascript Functions and Arrays
function functionName(parameters) {
// code to be executed
}
helloWorld(); function
helloWorld()
{ console.log("Hello,
world!"); }
In the example above, we are calling the helloWorld function before it has been
declared. However, because of function hoisting, the function declaration is
moved to the top of the scope, and the code runs without throwing any errors.
console.log(addSquares(2, 3)); // 13
console.log(addSquares(3, 4)); // 25
console.log(addSquares(4, 5)); // 41
Function declaration vs
Expression
• Function Declaration
A function declaration also known as a function statement declares a function with a function
keyword. The function declaration must have a function name.
Function declaration does not require a variable assignment as they are standalone constructs and
they cannot be nested inside a functional block.
These are executed before any other code.
The function in the function declaration can be accessed before and after the function definition.
Syntax:
function f1(paramA, paramB) {
// Set of statements
}
// Function Declaration
function f1(paramA, paramB) {
return paramA + paramB;
}
Function Definitions:
Syntax
function functionName(parameters)
{
// code to be executed
}
Example:
let array = [1, 2, 3, 4, 5];
array.reverse(); Output
console.log(array); let array = [1, 2, 3, 4, 5]; [ 5, 4, 3, 2, 1 ]
array.reverse();
console.log(array);
The values() method returns a new Array Iterator object that contains the values
for each index in the array.
Syntax:
Array.values()
Example:
const fruits = ["Apple", "Banana", "Cherry"];
// Get the iterator object Output:
Apple
const iterator = fruits.values(); Banana
Cherry
// Iterate over the values using the iterator
for (const value of iterator) {
console.log(value);
}
Example:
// Original Array
let courses = ["HTML", "CSS", "JavaScript", "React"];
// Converting array ot String
let str = courses.toString();
console.log(str); Output
HTML,CSS,JavaScript,Rea
ct
The delete operator is used to delete the given value which can be an object,
array, or anything.
Syntax:
delete object // or delete object.property // or delete object['property']
Example:
Deleting the given object’s property by using the delete operator.
let emp = {
firstName: "Raj",
lastName: "Kumar",
Output
salary: 40000 true
} { firstName: 'Raj', lastName:
'Kumar' }
console.log(delete emp.salary);
console.log(emp);
The flat() method is used to flatten the array i.e. it merges all the given array
and reduces all the nesting present in it.
Syntax:
arr.flat([depth])
Example:
// Creating multilevel array
const arr = [['1', '2'], ['3', '4', '5',['6'], '7']];
Output
// Flat the multilevel array [ '1', '2', '3', '4', '5', '6', '7']
const flatArr= arr.flat(Infinity);
console.log(flatArr);
strArr.unshift("sunil", "anil");
console.log(strArr);
Syntax:
Array.pop()
Example:
Output
let numArr = [20, 30, 40, 50]; [ 20, 30, 40 ]
numArr.pop(); [ 'amit', 'sumit' ]
console.log(numArr);
let strArr = ["amit", "sumit", "anil"];
strArr.pop();
console.log(strArr);
console.log(sub); Output: 3
Example:
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];