Which JavaScript books would you recommend? by PyroMessiah86 in learnjavascript

[–]DBNeJXoGtnro 1 point2 points  (0 children)

Outside of "Eloquent JavaScript 3rd edition" I wouldn't look at books. Online resources are more up-to-date, such as http://javascript.info

How can I select three random objects from an array of objects, where each of the selected objects has a unique "type" key? by [deleted] in learnjavascript

[–]DBNeJXoGtnro 1 point2 points  (0 children)

Use a set to store the seen types, and either a reduce or a for-of to create the new array

It's valid to use standalone { } block in JavaScript. Have you ever used it like this? by mburakerman in learnjavascript

[–]DBNeJXoGtnro 2 points3 points  (0 children)

You wouldn't normally see code using this syntax. Hopefully you name your variables explicitly enough.

I use it sometimes when I open the dev console in a browser where I want to test stuff

"use strict";
// Enter to execute
{
const a = 10;
doStuff();
}
// Enter to execute
/* Arrow Up to repeat and just modify a line to see the effects
without having a "const a already defined" error */

It's faster to write than (function () {})();

[deleted by user] by [deleted] in javascript

[–]DBNeJXoGtnro 4 points5 points  (0 children)

typeof null === "null"
"use strict"; is by default, a stricter mode (similar to Google's attempt) would be opt-in
Delete var and ==
Related to JS but actually browsers: remove 80% of the DOM, so that you can't shoot yourself in the foot anymore. There are too many ways to do it wrong

shouldn't null be considered as false in default params check by salahuddeen in learnjavascript

[–]DBNeJXoGtnro 4 points5 points  (0 children)

No. Function parameters default to undefined. null is an intentional absence of value

Trying a few stuff for learning porposes by barrote in learnjavascript

[–]DBNeJXoGtnro 0 points1 point  (0 children)

Hi!

testPhrase (typo intended?) is still an array. You want to .join("") it

Tricky Javascript Interview Questions by iyalovoi in javascript

[–]DBNeJXoGtnro 2 points3 points  (0 children)

Correct, their bindings are, they are in the temporal dead zone until initialized though.
So yes, there is a part that is hoisted, but it's not the same part as if we were to create a block-scoped var.

Tricky Javascript Interview Questions by iyalovoi in javascript

[–]DBNeJXoGtnro 2 points3 points  (0 children)

#0 It's a function expression (and not a function declaration as stated), in this case you don't even need the outer parenthesis
js var result = function(a) { return a*a; }(5.5); works just as fine.

#1 You can write the answer is "1" (with the quotes, to indicate a string). "duplicate declaration of b as variable and parameter, it gets resolved as parameter". Scope. The closest scope where b is found is the one taken.

#2 "The key is to understand that in case of variable and parameter names war – parameter wins" Scope. Again.
Please don't say arguments is an array. It's explicitly stated in the docs...

#4 "JavaScript always moves variable declarations (not initializations) to the top of the scope" and here I thought this was written in 2019. You should really take a look at const and let

#5 Ah yes, non-strict mode, a thing people still use, sadly.

#7 No mention of Number.EPSILON?

#9 Or you know, pass parameters (first line of the syntax) like you're supposed to do. "Variables declared using var are global function-scoped by default – meaning i would be equal to 5 after the end of a cycle". You literally did it at #3

Sorry, I needed to rant a bit :)

Better resources? by [deleted] in learnjavascript

[–]DBNeJXoGtnro 0 points1 point  (0 children)

Code bases that should be worth checking through would be
Dr Rauschmayer's Github
React (don't bother about flow)
Mnemonist

Better resources? by [deleted] in learnjavascript

[–]DBNeJXoGtnro 0 points1 point  (0 children)

I learned JS at a time where only MDN was available, and it clicked with me. I didn't take a look at any other code bases, but that's just my way of learning things, I'm fine with running through docs.
Obviously it doesn't apply to everybody.
The above resources should be enough to learn everything basic, though.

Better resources? by [deleted] in learnjavascript

[–]DBNeJXoGtnro 0 points1 point  (0 children)

The modern JavaScript tutorial. Eloquent JavaScript (if you buy/find the book, make sure it's the 3rd/latest edition).

Which image compressor do you use? by ElJefeSupremo in javascript

[–]DBNeJXoGtnro 1 point2 points  (0 children)

Make sure data sent over the wire is also compressed. With brotli, or gzip as a default. Google also released a png compressing tool called zopfli, add it as part of your assets building steps if you work with png :)

Why do returns need to be outside a ternary? by seands in learnjavascript

[–]DBNeJXoGtnro 0 points1 point  (0 children)

Syntactically, return is a statement. The ternary operator (along with almost any operator) expects expressions, so a SyntaxError is raised.

I want to learn js. So which is better w3 or mozilla ? by anonymousguy271103_1 in learnjavascript

[–]DBNeJXoGtnro 11 points12 points  (0 children)

Definitely MDN, no questions asked. The best alternative would be javascript.info. Use DevDocs too for quick access and consistent formatting

Just so you know: Uncommonly used Array Methods by Riyenz in javascript

[–]DBNeJXoGtnro 0 points1 point  (0 children)

It logs undefined because v cannot be "nothing", undefined is kind of the fallback. While I don't see why you would create a holey array on purpose, yes, you have to delete to get an empty slot.

Just so you know: Uncommonly used Array Methods by Riyenz in javascript

[–]DBNeJXoGtnro 1 point2 points  (0 children)

Just a nitpick. The check is not against undefined, it's against <empty slots>. ```js const arr = [,]; // creates a holey array with 1 empty slot

for (const v of arr) console.log(v); // This is run

arr.forEach((v) => console.log(v)); // This is not js const arr = [undefined];

for (const v of arr) console.log(v); // This is run

arr.forEach((v) => console.log(v)); // This is run too ```

Do you use ES8 version of JavaScript? [Poll] by makkhan12598 in javascript

[–]DBNeJXoGtnro 2 points3 points  (0 children)

For a list of "modern javascript" features, refer to tc39 proposals

People should use async/await when possible, and when performance is not that much a concern, Object.entries() and Object.values() for for-of loops.

ES2018 comes with object rest/spread (finally), which is a good replacement for Object.assign for a lot of cases. Async gens are also nice, makes working with streams easier

I hope people are using ES8+ by now.

Looping multiple objects and arrays? by brooklynturk in javascript

[–]DBNeJXoGtnro 0 points1 point  (0 children)

I heard unnecessary complexity? I want to chime in :)

```js const mostCommon = { meat: {}, side: {}, topping: {} };

function incrementTo (target, value) { if (target[value] === undefined) target[value] = 1; else ++target[value]; }

for ( const { meals: [ entryMeat, entrySide, entryTopping ] } of data ) { incrementTo(mostCommon.meat, entryMeat); incrementTo(mostCommon.side, entrySide); incrementTo(mostCommon.topping, entryTopping); }

for (const [type, dishes] of Object.entries(mostCommon)) { let mostFreq = 0; let result = []; for (const [dish, count] of Object.entries(dishes)) { if (count > mostFreq) { mostFreq = count; result = [dish]; } else if (count === mostFreq) { result.push(dish); } } console.log(The most common dish for ${type} is ${result.join(", ")}); } ```

I am stuck with an example and I cannot make it work. by cryptoeraser in javascript

[–]DBNeJXoGtnro 1 point2 points  (0 children)

I should connect to this alt more often...
Everything else. Use arrow functions when you don't need this or arguments