javascript
27 JanuaryGet String Between Two Characters in JavaScript
💡TL;DR Use the `substring()` method to get String between two characters in JavaScript. function getSubstring(str, start, end) { char1 = str.indexOf(start) + 1; char2 = str.lastIndexOf(end); return str.substring(char1, char2); } let originalString = "Hello,World!"; let substring = getSubstring(originalString, ',', '!'); console.log(substring); World Here, we got String between `,` and `!` in above example. Using substring() […]
27 DecemberReturn Boolean from Function in JavaScript
Using the Boolean() Function To get a Boolean from a function in JavaScript: Create a function which returns a Boolean value. Use the Boolean() function to get a Boolean value from the function created in the previous step. function func(){ return Boolean(10>5); } console.log(func()); true We used the Boolean(), a built-in function in JavaScript, to […]
23 DecemberCreate Array from 1 to 100 in JavaScript
Use for Loop To create the array from 1 to 100 in JavaScript: Use a for loop that will iterate over a variable whose value starts from 1 and ends at 100 while the variable’s value is incremented by 1 in each iteration. Inside the for loop, use the push() method to insert a value […]
23 DecemberGet Index of Max Value in Array in JavaScript
Using indexOf() with Math.max() Method To get an index of the max value in a JavaScript array: Use the Math.max() function to find the maximum number from the given numbers. Here, we passed an array and used the spread operator (...) to expand the array into individual elements. Use the .indexOf() function to get the […]
22 DecemberUpdate Key with New Value in JavaScript
Using Bracket Notation We can use bracket notation to update the key with the new value in Python. Update Single Key with New Value To update a single key with a new value, use bracket notation to read it and update it with a new value. let user = { "name": "Anonymous", "email": "@gmail.com", "age": […]
21 DecemberFormat Phone Number in JavaScript
Using match() Method We use the match() method to format a phone number in JavaScript. Format Without Country Code To format the phone number without country code: Use the replace() method with a regular expression /\D/g to remove non-numeric elements from the phone number. Use the match() method with a regular expression to match and […]
21 DecemberRemove Word from String in JavaScript
Using replace() Method We can use the replace() method to remove a word or words from a string in JavaScript. Remove a Single Occurrence of a Word To remove a single occurrence of a word from a string, use the replace() method to replace the word in the string with an empty string. var my_string […]
18 DecemberWrite Array to CSV in JavaScript
Using toString() method To write array to CSV in JavaScript: Declare an array with some elements in it. Use the toString() method. This method will convert the array into the CSV(Comma-Separated Value). Finally, console the value to see the result. Follow the below program: const myArray = ['Cricket', 'ICC', 'Football', 'FIFA'] const arrayToCsv = myArray.toString() […]
04 DecemberFill Array with Random Numbers in JavaScript
Using Array.from() and Math.random() method To fill array with random numbers in JavaScript: Use Math.random() to get random numbers between 0 to 1. Use array.from() with length with desired length and mapping function as () => Math.random() * 20. This mapping function will be executed for every element in the array. Let’s say we want […]
- 1
- 2
- 3
- …
- 5
- Nextkeyboard_arrow_right