Live data from Hacker News

A re-introduction to JavaScript

developer.mozilla.org

11–20 of 115 posts

Re: A re-introduction to JavaScript

#11

Very nice to see it starts off with a (correct) discussion of JS types! JS devs mostly don't know the actual types in JS, which is pretty crazy if you think about it.

I think 99% of the problems people have with Javascript are not actually types, but `this`. It's the most confusing part about Javascript. This context, var foo scope, what? Why is this value not updating? undefined? What???

Especially when you need to do something like:

  var that = this;

Re: A re-introduction to JavaScript

#12
> You can also use the unary + operator to convert values to numbers:

    + "42"; // 42
> [...] However the "+" operator simply converts the string to NaN if there is any invalid character in it.

Being a bit pedantic here, why not recommending the Number function which may be less obscure for beginners?

    Number("42"); // 42

Re: A re-introduction to JavaScript

#13

Earlier quoted context omitted.

I think 99% of the problems people have with Javascript are not actually types, but `this`. It's the most confusing part about Javascript. This context, var foo scope, what? Why is this value not updating? undefined? What???

Especially when you need to do something like: var that = this;

That's not generally necessary in ES6 when you use "=>" to define functions. Javascript is getting better...

Re: A re-introduction to JavaScript

#14
post #8

Earlier quoted context omitted.

Apart from they wouldn't really be "news", which is supposedly what this site is for (as denoted by the site's name and URL).

https://news.ycombinator.com/newsguidelines.html

"Please don't insinuate that someone hasn't read" the Hacker News Guidelines...

Re: A re-introduction to JavaScript

#15

Very nice to see it starts off with a (correct) discussion of JS types! JS devs mostly don't know the actual types in JS, which is pretty crazy if you think about it.

I think 99% of the problems people have with Javascript are not actually types, but `this`. It's the most confusing part about Javascript. This context, var foo scope, what? Why is this value not updating? undefined? What???

I agree, `this` is a disaster. But, I just can't hardly think of another language where the people working in it mostly don't know the types. (Well, OK, PHP devs probably mostly don't)

And I don't just mean run-of-the-mill blub programmers who dabble in jQuery, I mean some of the best devs I've ever seen in any language. I had a self-proclaimed JS expert tell me that JS has integers and floats as distinct types. Even the people I work with every day who are fantastic did not know that function and array are not types.

Of course, things like `typeof` don't help the situation, so I don't put the blame squarely on programmers' shoulders. And obviously you can get a lot done without knowing that, technically, functions are just objects that can be called. But it still strikes me as kinda crazy.

Re: A re-introduction to JavaScript

#16
post #12

> You can also use the unary + operator to convert values to numbers: + "42"; // 42 > [...] However the "+" operator simply converts the string to NaN if there is any invalid character in it. Being a bit pedantic here, why not recommending the Number function which may be less obscure for beginners? Number("42"); // 42

the String function is also a nice way to cast a value to a string (using .toString() is unsafe because it may not exist, such as with undefined and null): String(42) === "42".

An other thing that could maybe be covered is this specifity:

    typeof "42" === "string"
    typeof String(42) === "string"
    typeof new String(42) === "object"
Native constructors are ... fun beasts, to say the least.

Re: A re-introduction to JavaScript

#17
post #12

> You can also use the unary + operator to convert values to numbers: + "42"; // 42 > [...] However the "+" operator simply converts the string to NaN if there is any invalid character in it. Being a bit pedantic here, why not recommending the Number function which may be less obscure for beginners? Number("42"); // 42

Yes, that's a good suggestion. Boolean works as well, for coercing to boolean of course.

Re: A re-introduction to JavaScript

#18
The article mentions an idiom for iterating over an array. It says 'an even nicer idiom is...' and then it shows it.

I just wanted to say that the behaviour of this 'nicer' idiom may not be what's expected - it stops iterating once it hits the first falsy value. I was quite excited actually when I first saw them idiom, until I quickly realized its limitations

Re: A re-introduction to JavaScript

#20
post #12

> You can also use the unary + operator to convert values to numbers: + "42"; // 42 > [...] However the "+" operator simply converts the string to NaN if there is any invalid character in it. Being a bit pedantic here, why not recommending the Number function which may be less obscure for beginners? Number("42"); // 42

Since calling constructor functions without `new` is generally an error, I'd rather use `parseFloat`.
Post reply on HN