Thanks! Yeah, those quirks with different browsers... MDN references this polyfill for String#padStart and String#padEnd: github.com/behnammodi/polyfill/blo...
Thanks for the great post! Is there a way to easily unstringify text with a JavaScript? A kind of opposite of the stringify function? eg. how to remove the quotation marks in a dictionary of key-value pairs like {"name":"Tim", "age": "22"}. In this case, removing them just from the age value?
Because + operator tells JavaScript to wrap both sides of it with Number(). Left side here is blank, so Number() gives you 0. Right side here is "22", so Number("22") gives you 22. Then + operator adds these 2 Numbers together and gives you 22.
That's a lazy way to convert a String to a Number.
The explicit equivalent way is const stringToNumber = Number(string).
However, if you need more fine-tuned control over the parsing of the string, consider using parseInt or parseFloat (which lets you parse number representation in bin, oct, hex, etc., not just dec)
Hi, I'm a seasoned full-stack software engineer building web apps end-to-end. Helped startups launch MVPs and revamped Ui/Ux for established companies.Seeking to tackle your toughest technical challen
Note, splice is used for modifying arrays. I think you mean slice.
At first glance, there is no difference between word.substr(1) and word.slice(1). Both return a substring of word. However, their signature differs. The second parameter to substr is length. So, using substr(1, 5) means, take 5 characters starting at 1. In contrast, slice(1, 5) means take the characters between index 1 (inclusive) and index 5 (exclusive).
Besides, there's also a substring method. This behaves pretty much like slice. It takes a starting index and an optional ending index.
All three methods are valid ways to extract a substring. They may differ a bit in performance. Yet, the difference is more or less negligible. Choose what you find adequate.
Hi, I'm a seasoned full-stack software engineer building web apps end-to-end. Helped startups launch MVPs and revamped Ui/Ux for established companies.Seeking to tackle your toughest technical challen
Hello, may I translate your article into Chinese?I would like to share it with more developers in China. I will give the original author and original source.
I like this post although a struggle I had recently was that
padStartandpadEndare not supported on IE.I tried to find polyfill but there is not an official one so I ended up in a custom solution.
Thanks! Yeah, those quirks with different browsers... MDN references this polyfill for
String#padStartandString#padEnd: github.com/behnammodi/polyfill/blo...Back when IE mattered
Thanks for the great post! Is there a way to easily unstringify text with a JavaScript? A kind of opposite of the stringify function? eg. how to remove the quotation marks in a dictionary of key-value pairs like {"name":"Tim", "age": "22"}. In this case, removing them just from the age value?
Thanks for the great article again!
So you basically just want to convert string to a number?
Why does this work like this do you know?
Because
+operator tells JavaScript to wrap both sides of it withNumber(). Left side here is blank, soNumber()gives you 0. Right side here is "22", soNumber("22")gives you 22. Then+operator adds these 2Numbers together and gives you 22.That's a lazy way to convert a String to a Number.
The explicit equivalent way is
const stringToNumber = Number(string).However, if you need more fine-tuned control over the parsing of the string, consider using parseInt or parseFloat (which lets you parse number representation in bin, oct, hex, etc., not just dec)
For point 6.
whats the difference between
word.substr(1) vs word.splice(1) ?
Note,
spliceis used for modifying arrays. I think you meanslice.At first glance, there is no difference between
word.substr(1)andword.slice(1). Both return a substring ofword. However, their signature differs. The second parameter tosubstrislength. So, usingsubstr(1, 5)means, take 5 characters starting at 1. In contrast,slice(1, 5)means take the characters between index 1 (inclusive) and index 5 (exclusive).Besides, there's also a
substringmethod. This behaves pretty much likeslice. It takes a starting index and an optional ending index.All three methods are valid ways to extract a substring. They may differ a bit in performance. Yet, the difference is more or less negligible. Choose what you find adequate.
Got it, Thanks Kai it's very helpful
Hello, may I translate your article into Chinese?I would like to share it with more developers in China. I will give the original author and original source.
Sure! I hope it's helpful for some developers in China :)