Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  129] [ 2]  / answers: 1 / hits: 21504  / 10 Years ago, sun, july 27, 2014, 12:00:00

I'm new to Javascript, and I'm trying to write a chrome extension.



For the extension, if users input a string, I want the first two characters to be sliced off.



Currently, I have:



if (text.charAt(0) == '/') {
text.slice(0,2);
chrome.tabs.create({
url: PrivateURL + text
});
}


But it's not working, nothing is being sliced off. I have a feeling something is wrong with my syntax, as I'm still learning. Any help is appreciated.


More From » javascript

 Answers
21

Slice does not work in place but returns the value:



var text = Hello World;
console.log(text.slice(0,2));
// He
console.log(text);
// Hello World (unchanged!)


You need to assign it to the variable like this:



text = text.slice(0,2);


Also, from your question, it sounds as if you're trying to remove the first two letters. That would be



var text = Hello World;
text = text.slice(2);
// will be llo World

[#70024] Thursday, July 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maxd

Total Points: 568
Total Questions: 100
Total Answers: 101

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;