You have to use negative index in String.prototype.slice() function.

  • using negative index as first argument returns the sliced string to the 6 elements counting from the end of the string.

var example = "javascript";
console.log(example.slice(-6)); 

  • using negative index as the second argument returns the sliced string from 0 to the 6th element counting from the end. It's opposite to the first method.

var example = "javascript";

console.log(example.slice(0, -6));

In your particular case, you have to use the second method.

console.log('11001000000000000001010'.slice(0, -18));

console.log('110000000001101011100'.slice(0, -18));

If you want to read more about that function, visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

Answer from kind user on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › slice
String.prototype.slice() - JavaScript | MDN
The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.
🌐
W3Schools
w3schools.com › jsref › jsref_slice_string.asp
JavaScript String slice() Method
The slice() method returns the extracted part in a new string.
Discussions

Why do we have a String.slice() method in JS ?
https://stackoverflow.com/a/2243835 This answer outlines the commonalities and differences between the two. More on reddit.com
🌐 r/learnjavascript
3
2
January 4, 2021
Help with String Slice - total beginner
Hi, I dont understand why my code is being sliced at character 0 rather than character 5. When if I console.log position I get the character I want. Please help I know this perhaps isnt the most concise way to do this but I’m trying to write everything out step by step so I can check as I ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
June 5, 2021
javascript - What is the difference between String.slice and String.substring? - Stack Overflow
Take a look at this MDN section .../docs/Web/JavaScript/Reference/… ... If either argument is greater than the string's length, the string's length will be used instead. ... If start > stop, then substring will swap those 2 arguments. If either argument is negative or is NaN, it is treated as if it were 0. ... If start > stop, slice() will return ... More on stackoverflow.com
🌐 stackoverflow.com
If you could only use string.slice or string.substring, which string method would you choose?
.slice() seems to have become more popular. Advantages: Accepts negative values as parameters. Same name as Array.protoype.slice() Differences between the two: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring#Differences_between_substring_and_slice More on reddit.com
🌐 r/learnjavascript
1
3
November 24, 2019
🌐
Mimo
mimo.org › glossary › javascript › string-slice
JavaScript String slice() Method: Syntax, Usage, and Examples
Learn how to use the JavaScript slice() method to extract parts of a string for trimming, parsing, formatting, and building clean, immutable text logic.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Help with String Slice - total beginner - JavaScript - The freeCodeCamp Forum
June 5, 2021 - Hi, I dont understand why my code is being sliced at character 0 rather than character 5. When if I console.log position I get the character I want. Please help I know this perhaps isnt the most concise way to do this but I’m trying to write everything out step by step so I can check as I go. function secure (email){ let whereAt = email.search("@"); let position = email.charAt(5); let substring = email.slice(position, whereAt); let ans = email.replace(substring, ".........."); ...
Find elsewhere
🌐
CoderPad
coderpad.io › blog › development › 4-ways-to-use-javascript-slice-method
4 Ways to Use JavaScript .slice() Method and How to Do Each - CoderPad
June 7, 2023 - The method can be used to quickly ... only a subset of data. Developers can use the .slice() method to create a new array or string based on an existing one....
Top answer
1 of 9
1234

slice() works like substring() with a few different behaviors.

Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);

What they have in common:

  1. If start equals stop: returns an empty string
  2. If stop is omitted: extracts characters to the end of the string
  3. If either argument is greater than the string's length, the string's length will be used instead.

Distinctions of substring():

  1. If start > stop, then substring will swap those 2 arguments.
  2. If either argument is negative or is NaN, it is treated as if it were 0.

Distinctions of slice():

  1. If start > stop, slice() will return the empty string. ("")
  2. If start is negative: sets char from the end of string, exactly like substr().
  3. If stop is negative: sets stop to: string.length – Math.abs(stop) (original value), except bounded at 0 (thus, Math.max(0, string.length + stop)) as covered in the ECMA specification.

Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()

2 of 9
190

TL;DR:

  • If you know the index (the position) on which you'll stop (but NOT include), use slice().
  • If you know the length of characters to be extracted, you could use substr(), but that is discouraged as it is deprecated.

Otherwise, read on for a full comparison

Syntax

  • string.slice(start,end)
  • string.substr(start,length)
  • string.substring(start,end)

Note #1: slice()==substring()

What it does?

  • slice() extracts parts of a string and returns the extracted parts in a new string.
  • substr() extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
  • substring() extracts parts of a string and returns the extracted parts in a new string.

Note #2: slice()==substring()

Changes the Original String?

  • slice() doesn't
  • substr() doesn't
  • substring() doesn't

Note #3: slice()==substr()==substring()

Using Negative Numbers as an Argument

  • slice() selects characters starting from the end of the string
  • substr() selects characters starting from the end of the string
  • substring() doesn't perform

Note #4: slice()==substr()

If the First Argument is Greater than the Second

  • slice() doesn't perform
  • substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems
  • substring() will swap the two arguments, and perform as usual

The First Argument

  • slice() required; starting Index
  • substr() required; starting Index
  • substring() required; starting Index

Note #5: slice()==substr()==substring()

The Second Argument

  • slice() optional; the position (up to, but not including) where to end the extraction
  • substr() optional; the number of characters to extract
  • substring() optional; the position (up to, but not including) where to end the extraction

Note #6: slice()==substring()

What if the Second Argument is Omitted?

  • slice() selects all characters from the start-position to the end of the string
  • substr() selects all characters from the start-position to the end of the string
  • substring() selects all characters from the start-position to the end of the string

Note #7: slice()==substr()==substring()

So, you can say that there's a difference between slice() and substr(), while substring() is basically a copy of slice().

If you want substr's functionality:

"foobarbaz".substr(index, length);

without using a deprecated feature, you can just do:

"foobarbaz".substring(index, length + index);

And get the exact same results bar all of the edge-cases, like negative index/length.

🌐
Medium
medium.com › nerd-for-tech › basics-of-javascript-string-slice-method-79076eb1bac0
Basics of Javascript · String · slice() (method) | by Jakub Korch | Nerd For Tech | Medium
June 19, 2021 - The slice() method extracts parts of a string and returns the extracted parts in a new string. The first parameter is the mandatory and specifies start position for the extraction to take place.
🌐
W3Schools
w3schools.com › js › js_string_methods.asp
JavaScript String Methods
1 week ago - Slice out a portion of a string from position 7 to position 13: let text = "Apple, Banana, Kiwi"; let part = text.slice(7, 13); Try it Yourself » · JavaScript counts positions from zero.
🌐
Refine
refine.dev › home › blog › tutorials › how to use javascript slice method
How to Use JavaScript Slice Method | Refine
November 4, 2024 - Working with the arguments Object: In functions, to convert the arguments object into a real array, slice can be used. This allows you to use Array methods on arguments. In this section, we explore the String version of JavaScript slice() method with examples of creating substrins from a source string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
substr()'s start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0. Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start. Furthermore, substr() is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible. ... const text = "Mozilla"; console.log(text.substring(2, 5)); // "zil" console.log(text.substr(2, 3)); // "zil" The substring() and slice() methods are almost identical, but there are a couple of subtle differences between the two, especially in the way negative arguments are dealt with.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-slice-method
JavaScript String slice() Method - GeeksforGeeks
July 11, 2025 - The slice() method in JavaScript is used to extract a portion of a string and create a new string without modifying the original string.
🌐
Can I Use
caniuse.com › mdn-javascript_builtins_string_slice
JavaScript built-in: String: slice | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
🌐
React
react.dev › learn › updating-arrays-in-state
Updating Arrays in State – React
Sometimes, you may want to insert ... the ... array spread syntax together with the slice() method. The slice() method lets you cut a “slice” of the array....
🌐
Attio
attio.com › engineering › blog › javascript-string-slice-considered-harmful
JavaScript string slice() considered harmful | Attio
Unfortunately, the string .slice() method we've been using thus far does work in terms of code units, not code points.
🌐
Programiz
programiz.com › javascript › library › string › slice
JavaScript String slice()
// slice the substring from index 0 to 10 let result = message.slice(0, 10); console.log(result); // Output: JavaScript ... Here, str is a string.
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
break class const continue debugger do...while for for...in for...of function if...else let return switch throw try...catch var while JS Strings · at() charAt() charCodeAt() codePointAt() concat() constructor endsWith() fromCharCode() includes() indexOf() isWellFormed() lastIndexOf() length localeCompare() match() matchAll() padEnd() padStart() prototype repeat() replace() replaceAll() search() slice() split() startsWith() substr() substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() toWellFormed() trim() trimEnd() trimStart() valueOf() JS Typed Arrays
🌐
Squash
squash.io › javascript-substring-splice-and-slice-the-complete-guide
How to Use Javascript Substring, Splice, and Slice
May 1, 2023 - Javascript slice is a method used to extract a portion of an array or string and return it as a new array or string. It takes two arguments: the starting index and the ending index.
🌐
Prism
prismjs.com
Prism
Requiring prismjs will load the default languages: markup, css, clike and javascript. You can load more languages with the loadLanguages() utility, which will automatically handle any required dependencies. ... const Prism = require('prismjs'); const loadLanguages = require('prismjs/components/'); loadLanguages(['haml']); // The code snippet you want to highlight, as a string const code = `= ['hi', 'there', 'reader!'].join " "`; // Returns a highlighted HTML string const html = Prism.highlight(code, Prism.languages.haml, 'haml');