This post was originally published at kais.blog.
Let's move your learning forward together! Follow me on Twitter for your daily dose of developer ...
For further actions, you may consider blocking this person and/or reporting abuse
I like this post although a struggle I had recently was that
padStart
andpadEnd
are 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#padStart
andString#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 2Number
s 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,
splice
is 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 tosubstr
islength
. 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
substring
method. 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 :)