Javascript

JavaScript Program to Replace All Occurrences of a String

replace() method returns a new string with the specified string replaced. Let’s see an example. Syntax of replace function is given below. Syntax : string.replace(pattern, replacement) const message = "abc xyz"; // replace the first b with c let result = message.replace(‘b’, ‘c’); console.log(result); //Output: acc xyz We can replace also the first occurrence in …

JavaScript Program to Replace All Occurrences of a String Read More »

Get a random Element from an Array using JavaScript

We can use Math.random , math.floor functions to generate random array items. Length property returns the number of elements in the array. Math.floor() function rounds down a number to the next smallest integer. Math.random() function returns a random number between 0 and 1.  Let’s see the below example const array=[1,2,3,4,5]; const random1 = array[(Math.floor(Math.random() * …

Get a random Element from an Array using JavaScript Read More »

Useful Array searching methods in Javascript

includes()  Method In JavaScript, includes() method determines whether a string contains the given characters within it or not. This method returns true if the string contains the characters, otherwise, it returns false. const array = [50, 22, 31, 20, 9]; const check = array.includes(20); const check1 = array.includes(33); console.log(check) console.log(check1) //Output: true false find()  Method …

Useful Array searching methods in Javascript Read More »

The Array Iterators Cheatsheet for JavaScript

Iterators are methods that are called on arrays to manipulate elements and return some values.The most frequently used iteration methods are forEach(), filter(), map(), reduce(),every(), some(), find() , findindex(). forEach() Method The forEach() method is used to loop over elements of an array, it takes in a callback function as an argument which, in turn, …

The Array Iterators Cheatsheet for JavaScript Read More »

Learn Javascript Object Methods with examples

Objects in JavaScript are collections of key/value pairs. The values can consist of properties and methods and may contain all other JavaScript data types, such as strings, numbers, and Booleans. toString()  The toString() method takes no arguments; it returns a string that somehow represents the value of the object on which it is invoked. JavaScript …

Learn Javascript Object Methods with examples Read More »

Scroll to Top