5 ms·
> basic null check What's funny to me is that leftpad(null, 6, ' ') outputs " null".
by rbobby 11y ago
> basic null check
What's funny to me is that leftpad(null, 6, ' ') outputs " null".
- ansgri 11y agoAm I right that len(" null") here is 5, so it's working incorreclly?
- rbobby 11y agoThought it was a typo on my part... but HN seems to be trimming running spaces.
- talideon 11y agoHN isn't: your browser is. Runs of whitespace are collapsed in HTML to single spaces unless specifically styled otherwise. You can verify that they're there by viewing the page source.
- deadowl 11y agoI like to use non-breaking spaces for that kind of thing, although they still might collapse that? At least it doesn't wrap non-breaking spaces.
- talideon 11y agoNo, they wouldn't collapse that: non-breaking spaces aren't considered whitespace in HTML. The side effect of that is that if you copy some text with non-breaking spaces, you'll get non-breaking spaces, so something that looks as if it contains spaces won't necessarily behave as such. In HN, if you need to quote something where space is significant, you're best off formatting it as a preformatted block by indenting it: Here is some preformatted text. It might break up the flow of the text, but if something like whitespace is significant, that's probably a good thing.
- daveloyall 11y agoI think the joke here is that javascript has a long history of thinking that this: null is the same as this: "null" ...Which it isn't.