substr vs substring vs slice

These three methods differ in the parameters, but results in the same primitive string. You can stick to one at all times, or pick the best suited one for each use case.

When passing in a negative value as first parameter they handle it differently:

substr

Some older interpreters (IE8) will treat a negative start index as 0.

substring

The smallest value will be used as start index. A negative start index will be treated as 0.

slice

A negative start index is acceptable, and will start counting from the end.

Performance stats at jsperf.com

The constructors

String.substr(begin, [length])

Starts from begin position and reads length amonut of characters where stop can not be a negative value.

String.substring(start, [stop])

Starts from start position and stops at stop position where stop can not be a negative value.

String.slice(begin, [end])

Starts from begin position and ends at end position where end can be a negative value counting from the end.


Some examples that produce the same result, but have a different set of parameters in the call.

var str = 'Hello World'; console.log(str.substr(1)); // ello World console.log(str.substring(1)); // ello World console.log(str.slice(1)); // ello World console.log(str.substr(-1)); // d console.log(str.substring(10)); // d console.log(str.slice(-1)); // d console.log(str.substr(1, 9)); // ello Worl console.log(str.substring(1, 10)); // ello Worl console.log(str.slice(1, -1)); // ello Worl console.log(str.substr(-4, 3)); // orl console.log(str.substring(7, 10)); // orl console.log(str.slice(-4, -1)); // orl console.log(str.substr(0, 1)); // H console.log(str.substring(0, 1)); // H console.log(str.slice(0, 1)); // H

Be aware, all of these can run into issues with multibyte characters.

var str = 'हिं'; console.log(str); // हिं console.log(str.length); // 3 console.log(str.substr(0, 1)); // ह console.log(str.substring(0, 1)); // ह console.log(str.slice(0, 1)); // ह console.log(str.substr(-2, 1)); // ि console.log(str.substr(1, 1)); // ि console.log(str.substring(1, 2)); // ि console.log(str.slice(-2, 2)); // ि console.log(str.slice(1, 2)); // ि console.log(str.substr(-1)); // ं console.log(str.substr(2)); // ं console.log(str.substring(2)); // ं console.log(str.slice(-1)); // ं console.log(str.slice(2)); // ं
Comparing to php

Javascript does not have an equal method of php's substr function.

The substr method lets you work with a length parameter, just like php, but will not accept a negative length. For these cases you can use slice.

Example php code

$str = 'Hello World';
$result = substr($str, 3, 2);     // lo
$result = substr($str, 1, -2);    // ello Wor
$result = substr($str, -5, -2);   // Wor

To achieve the same in Javascript you use substr for the first one, and slice for the second one.

var str = 'Hello World'; console.log(str.substr(3, 2)); // lo console.log(str.slice(1, -2)); // ello Wor console.log(str.slice(-5, -2)); // Wor

You might also want to look at substr function at phpjs.org.