Get the first two characters of string JavaScript | Example code

Get first two characters of string JavaScript HTML example code You can use inbuilt slice method to get first 2 characters of a string in JavaScript. Pass index value 0 for start and 1 for end.

Output: Do comment if you have any doubts and suggestions on this JS char topic.


Get the middle character JavaScript | Extracting middle of a string example

Get the middle character JavaScript Example code: Let’s Create a function that returns the middle character/s of a sentence.

Output: Do comment if you have any doubts and suggestions on this example code.


JavaScript split a sentence into an array of words | Example code

Just use the split method to convert sentences into an array of words in JavaScript. You have to use a separator which uses to indicating where each split should occur. We will use a space separator, you can use a comma or another. JavaScript split a sentence into an array of words Example HTML example Read More…


JavaScript split a sentence into words | Example code

Just use the split method to split a sentence into words in JavaScript. You can also use it for loop iteration. In both ways, you have to use the space option for separating normal sentences. JavaScript split the sentence into words Example code HTML example code:- Using split method It will return array without space. Read More…


Loop through words in a string JavaScript | Iterate words in a string in JS

Simple for loop can use for in Loop through words in a string JavaScript. And for iterate words in string use split, map, and join methods (functions). Loop through words in a string JavaScript Example

Output: Iterate words in string in JS Example code

Output: Do comment if you have any doubts and Read More…


Get word from string JavaScript | Extract a specific word example

Use split and some function or regex with word boundaries to find or Extract a specific word from string in JavaScript. Get word from string JavaScript Example HTML example code: Match if word present in given string. Using split and some function If word found then return true.

Using a regex with word boundaries: Read More…


Get the first word of string JavaScript | Example code

Use split method with 0 index to get the Get the only first word of string in JavaScript. Split method return an array, so you can easily get first element.

Get the first word of string JavaScript example HTML example code: You need to split the string by space and the get only 0 Read More…


Get the last word from string JavaScript | Example code

Use Regular expression or with replace and split method to get the the last word from string JavaScript. It’s not only way to get last word from string you can use split or inbuilt lastIndexof with substring method. Get the last word from string JavaScript example code HTML example code:- Regular expression A strips all Read More…


JavaScript remove the last word from a string | Example code

The simplest way to get remove the last word from a string in JavaScript Using string.substring() Function. There are many ways and methods. We will see the other method with examples in this tutorial. JavaScript remove the last word from a string Example HTML example code:- Using substring() Function¬† The lastIndexOf() function is used to Read More…


JS get a random element from the Array | Example code

You can get a random element from the Array in JS by using Math.random() function to get the random number then Multiply it by the array length. After it, you will get numbers between zero to array length. Last use Math.floor() to get the index number from 0 to arrayLength-1. JS get a random element Read More…