w3schools
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About
BEST WEB HOSTING JustHosts

JavaScript slice() method


String Object Reference JavaScript String Object

Definition and Usage

The slice() method extracts a part of a string and returns the extracted part in a new string. Otherwise it returns -1.

Syntax

stringObject.slice(begin,end)

Parameter Description
begin Required. Where to begin the extraction. Must be a number. Starts at 0
end Optional. Where to end the extraction. If omitted, slice() selects all characters from the begin position to the end of the string


Tips and Notes

Tip: You can use a negative numbers to select from the end of a string.


Example

Example

Extract different parts of a string:

<script type="text/javascript">

var str="Hello happy world!";

// extract all characters, start at position 0:
document.write(str.slice(0)+"<br />");

// extract all characters, start at position 6:
document.write(str.slice(6)+"<br />");

// extract from the end of the string, and to position -6:
document.write(str.slice(-6)+"<br />");

// extract only the first character:
document.write(str.slice(0,1)+"<br />");

// extract the characters from position 6 to position 11:
document.write(str.slice(6,11)+"<br />");

</script>

The output of the code above will be:

Hello happy world!
happy world!
world!
H
happy

Try it yourself »


String Object Reference JavaScript String Object

DreamTemplate


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
Top Web Hosting
Windows Hosting
WEB BUILDING
XML Editor – Free Trial!
FREE Flash Website
FREE Web Templates
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display