Home > UI/UX Development > JavaScript > Show specific number of words in string using JavaScript

Show specific number of words in string using JavaScript

This post was most recently updated on August 5th, 2024

<p id=”shortWord”></p>

<script>
var word = “Lorem Ipsum is simply dummy text of the printing and typesetting industry.”
var wordArr = word.split(” “);
var wordShort = wordArr.splice(0, 5).join(” “);
document.getElementById(‘shortWord’).innerHTML= wordShort;
</script>

Output: “Lorem Ipsum is simply dummy”

 

This Article is TAGGED in , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">