Skip to main content

Posts

Showing posts with the label javascript

JavaScript Arrow Function

In this example, you will learn about JavaScript arrow function. Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows you to create functions easy way compared to regular functions. Regular Function // regular function let result = function(a, b) { return a + b; } ES6 Arrow Function // arrow function let result = (a, b) => a + b; Arrow Function Syntax The syntax of the arrow function look like is: let myFunction = (arg1, arg2, ...argN) => { statement... } Here, myFunction is the function name arg1, arg2,...argN is the argument(parameter) of the function statement... is the body of the function

JavaScript Program to Remove Specific Item From an Array

How to Remove Specific Element From An Array In this example, you will learn to write a JavaScript program that will remove a specific element from an array. Example 1 : Using For Loop Remove Element From Array function removeElementFromArray(array, n) { const newArray = []; for ( let i = 0; i < array.length; i++) { if(array[i] !== n) { newArray.push(array[i]); } } return newArray; } const elements = [1, 2, 3, 4, 5]; const result = removeElementFromArray(elements, 4); console.log(result); Output [1,2,3,5] In the above program, an element 4 is removed from an array using a for loop. Example 2 : Using Array.splice() Remove Element From Array const elements = [1, 2, 3, 4, 5]; //remove element 3 const result = elements.splice(2,1); console.log(elements); Output [1, 2, 4, 5] In the above program, an element 3 is removed from an array using a splice() method.