TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript String slice() Method

JavaScript string slice() Method with example, by string literal, by string object (using new keyword), javascript string methods, charAt(index) method, concat(str) method, indexOf(str) method, lastIndexOf(str) method, lowerCase() method, upperCase() method, slice(beginIndex, endIndex) method, trim() method, toLowerCase(), slice(), concat(), charCodeAt() method etc.

<< Back to JAVASCRIPT

JavaScript String slice() Method

The JavaScript string slice() method is used to fetch the part of the string and returns the new string. It required to specify the index number as the start and end parameters to fetch the part of the string. The index starts from 0.

This method allows us to pass a negative number as an index. In such case, the method starts fetching from the end of the string. It doesn't make any change in the original string.

Syntax

The slice() method is represented by the following syntax:

string.slice(start,end)

Parameter

start - It represents the position of the string from where the fetching starts.

end - It is optional. It represents the position up to which the string fetches. In other words, the end parameter is not included.

Return

Part of the string

JavaScript String slice() Method Example

Let's see some simple examples of slice() method.

Example 1

Here, we will print the part of the string by passing starting and ending index.

<script>
var str = "TheDeveloperBlog";
document.writeln(str.slice(2,5)); 
</script>
Test it Now

Output:

vat

Example 2

Here, we will provide starting index only. In such case, the method fetches the string up to its length.

<script>
var str = "TheDeveloperBlog";
document.writeln(str.slice(0)); 
</script>
Test it Now

Output:

TheDeveloperBlog

Example 3

This is one more example where we provide only the starting index.

<script>
var str = "TheDeveloperBlog";
document.writeln(str.slice(4)); 
</script>
Test it Now

Output:

tpoint

Example 4

In this example, we will provide negative number as an index. In such case, the method starts fetching from the end of the string.

<script>
var str = "TheDeveloperBlog";
document.writeln(str.slice(-5));
</script>
Test it Now

Output:

point

Example 5

In this example, we will provide negative number as a starting and ending index. In such case, the method starts fetching from the end of the string.

var str = "TheDeveloperBlog";
document.writeln(str.slice(-5,-1));
Test it Now

Output:

poin

Next TopicJavaScript String




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf