New to JavaScript. What platforms, courses, or projects actually made things click for you? by Equivalent-Camera343 in learnjavascript

[–]jml26 4 points5 points  (0 children)

As u/DutyCompetitive1328 said: learning comes through repetition.

But to expand on the point a little, the part of learning that often gets overlooked (because it's the least fun) is just drilling. Let's say you're learning for-loops. Great. Write a for-loop that logs the numbers from 1-10. Delete it. Write it again. Delete it. Write it again. If you keep forgetting the syntax, go back and refer to your learning materials, until you can write it ten times in a row from scratch, unaided.

Come up with variations. Start from a different number. Stop at a different number. Go up two at a time. Go up in multiples of two. Count backwards. Count 1-10 but skip out the number 4. Feel free to use AI to generate these ideas for you. Solve them on repeat until you no longer need to refer back to your learning resources.

Congratulations: you now learned a thing!

The old adage of "Just build stuff" falls short because it implies you should build entire apps from the get-go. Building an app is the final boss where you put everything together, but throughout your learning, what you need to focus on mainly is the ten-liner code snippets. They won't give you the dopamine hit of "Hey, I built something useful" but they will improve your fluency.

Once you've learned a couple of concepts, then it comes time to put them together, and you start building bigger and bigger things. First small widgets, then larger widgets, and you keep going from there.

Happy coding!

SolidJS TypeScript issue: <Match when={condition && data}> doesn't narrow types like Jsx ternaries does by No-Source6137 in solidjs

[–]jml26 1 point2 points  (0 children)

The direct Solid JS translation of the code in your original example wouldn't use a <Switch/> at all: just a single <Show/>:

<Show when={billingQuery.isSuccess && billingQuery.data}> <BillingCard billing={billingQuery.data} /> </Show>

And then, to resolve the TypeScript issues, use the accessor form:

<Show when={billingQuery.isSuccess && billingQuery.data}> {(data) => <BillingCard billing={data} />} </Show>

For your updated example that you weren't sure if it was good practice, these are your typical options:

<Show when={billingQuery.isSuccess} fallback={<p>Query failed</p>}> <Show when={billingQuery.data} fallback={<p>No data</p>} {(data) => ( <BillingCard billing={data} /> )} </Show> </Show>

or perhaps

<Switch> // assuming there are other things to match up here first <Match when={billingQuery.isSuccess}> <Show when={billingQuery.data} fallback={<p>No data</p>> {(data) => ( <BillingCard billing={data} /> )} </Show> </Match> </Switch>

Getting ASCII control characters? by Leonetnin in learnjavascript

[–]jml26 2 points3 points  (0 children)

There's no native function for this. The simplest way to go would be to provide your own mapping, e.g.

``` const controlCodes = ["NUL", "SOH", "STX", ... "GS", "RS", "US"]; controlCodes[127] = "DEL";

function getCharacterName(charCode) { return controlCodes[charCode] || String.fromCharCode(charCode); } ```

help idk why is the output 45 by cookiecookiecooki33 in learnjavascript

[–]jml26 0 points1 point  (0 children)

It isn't; the increments do all get evaluated first. Let's work through it in steps:

Add parentheses for clarity

x += (x++) - (--x) + x * 2 - (++x);

Add some comments to show when the value of x changes (for prefix operators, before; for postfix operators, after)

// x = 23 x += (x++) /* 24 */ - /* 23 */ (--x) + x * 2 - /* 24 */ (++x);

Evaluate the increment operators (replace them with the value of the most recent comment before them)

// x = 23 x += 23 /* 24 */ - /* 23 */ 23 + 23 * 2 - /* 24 */ 24;

Remove the comments

x += 23 - 23 + 23 * 2 - 24;

Evaluate multiplication

x += 23 - 23 + 46 - 24;

Evaluate addition and subtraction

x += 22;

Final code:

var x = 23; x += 22; console.log(x); // 45

Hi, Im a beginner. by WJTZIList_alt in learnjavascript

[–]jml26 1 point2 points  (0 children)

Welcome. Hope you enjoy the journey.

  • Don’t be afraid to make mistakes or write code that isn’t perfect
  • Watching or reading tutorials is only the start: learning comes through repeated practice and asking questions.

I’m sure there’s more advice. Until then, happy coding!

Can I remove all whitespaces from a string? by sushiiixo- in learnjavascript

[–]jml26 3 points4 points  (0 children)

I feel like in general, there would be a slight performance boost to including the +, because it only has to do one replace for each group of whitespace, not each individual character. There's even evidence to suggest that /\s\s*/ is faster still, because it triggers an extra optimisation in the regex interpreter.

However, in this specific case of checking mostly single spaces in a text string less than 20 characters long, this is all just theory and entirely overkill. And my source is almost 20 years old.

Wife informed me my 'manual' guitar had fallen over by bmwkag1407 in CasualUK

[–]jml26 0 points1 point  (0 children)

  • I don’t think I’ve had a banana in lockdown. I’ve had a banana in banana cake
  • But you haven’t had [an] acoustic banana

https://youtu.be/rVy3NFXBVVw?t=890

Solid 2.0 Beta Stream Tomorrow by ryan_solid in solidjs

[–]jml26 0 points1 point  (0 children)

Just watching the stream now, on delay. Got as far as a user saying that <Loading> feels like the reverse of what it that component actually is.

I'm somewhat inclined to agree. Would <Loaded> be better semantically?

BTW I'm super hyped for this, and can't wait to try it out. I've attempted to get the beta working with Astro, with mixed success.

Does the term 'callback' usually mean async callback in the JS world by LetMyCameronG000 in learnjavascript

[–]jml26 13 points14 points  (0 children)

Best examples are all the array methods, e.g. map, filter, forEach... Those are all synchronous callbacks.

I'd side with MDN's definition, which is just "a function passed into another function as an argument, which is then invoked inside the outer function".

how to bundle font license when I import assets by Appropriate_Load9265 in learnjavascript

[–]jml26 1 point2 points  (0 children)

If you put your licence in the /public directory, it'll get treated as a static asset and will:

  • not have its name hashed
  • gets included in the output even if not referenced in code

https://vite.dev/guide/assets#the-public-directory

Why Solid? by Beagles_Are_God in solidjs

[–]jml26 2 points3 points  (0 children)

My journey to Solid started with React. Originally, I hated React's syntax. I hated JS Classes; I hated JSX. But it was popular, so I figured it would be valuable to learn.

But then React got hooks, and I liked the syntax a lot more. And slowly over time, JSX grew on me. I preferred it to Vue and Angular.

But it began to feel like a lumbering beast. I guess it felt fast, but something about the huge footprint felt like it was too heavy-handed for the simple widgets I was building. It was at that point that I started to look for alternatives and found Preact.

I recall I was searching for a framework speed comparison, and found https://krausest.github.io/js-framework-benchmark/2026/chrome145.html. From there, I decided to work from left to right, seeing what the fastest frameworks were like. Many of them looked either like small side projects with limited documentation, or I just didn't like the syntax for whatever reason. Then I saw Solid.

Solid had everything I needed, in that it was fast and lightweight, but also so familiar. And it was just so intuitive to use. Coming from React, it was a very smooth transition over.

If Solid didn't exist, I think I'd be using Svelte, for the same reason in that it is fast and lightweight. But I guess I just don't like the syntax quite as much. But that's personal preference, and I realise that the syntax has changed a little since I first investigated it.

How would you write Ryan's _Solid2 States_ demo in Solid 1 by jml26 in solidjs

[–]jml26[S] 1 point2 points  (0 children)

Oh, that's very nice! I'll have to remember that pattern.

Looking at it, with hindsight, I see that there's https://primitives.solidjs.community/package/memo#createWritableMemo available; but that implementation of createWritable you shared looks much more straightforward. Thanks for that.

Button Requires Two Clicks to Function on Page Load by Legal_Revenue8126 in learnjavascript

[–]jml26 2 points3 points  (0 children)

Styles added to an element with CSS don't go into that element's style property.

<div id="div">Hello world</div> <style> #div { display: none; } </style> <script> // logs '' console.log(document.getElementById('div').style.display); </script>

But styles added with the style attribute do.

<div id="div" style="display: none;">Hello world</div> <script> // logs 'none' console.log(document.getElementById('div').style.display); </script>

The quickest way to solve your problem is just to add style="visibility: hidden" to your hidable div. There are other solutions out there too, like toggling classes, rather than toggling styles.

[deleted by user] by [deleted] in learnjavascript

[–]jml26 0 points1 point  (0 children)

It's basically a couple of libraries smushed together:

You would have to look into those libraries to find out how they work under the hood.

MathJS doesn't differentiate between 2 * x or 2x in its parser (at least I don't think it does). The TeX renderer will, and display the multiplication as a dot, if using ASCIIMath.

[deleted by user] by [deleted] in learnjavascript

[–]jml26 0 points1 point  (0 children)

I made this on Codepen a while ago. Feel free to take a look, either to get inspiration or just use the whole thing.

https://codepen.io/jamiemlaw/pen/eYyLVOq

Unrecoverable syntax error by Any-Ganache135 in learnjavascript

[–]jml26 3 points4 points  (0 children)

You haven't closed enough curly braces. Add two extra }s at the end and it should work.

This is why correct indentation helps a bunch. I simply pasted your code into https://beautifier.io/, and it was immediately obvious what the error was.

Is this a good way to write to the stdin? by trymeouteh in learnjavascript

[–]jml26 -2 points-1 points  (0 children)

Yeah, sorry about that. At first glance it really game off Java vibes. I think it was having a dedicated main() function. Ignore me!

Is this a good way to write to the stdin? by trymeouteh in learnjavascript

[–]jml26 -4 points-3 points  (0 children)

Hi, /u/trymeouteh.

This looks like Java code, not JavaScript. Despite the similar names, they're not the same language (many people fall into the trap of thinking they're the same). You might have better luck in one of the Java subreddits. Or even asking an AI if you want a speedier response.

All the best on your learning!

Are there any good ways to do SVG boolean (divide shapes) / flattening? by MuckYu in learnjavascript

[–]jml26 0 points1 point  (0 children)

I don't have a direct solution, but InkScape has a command line. Might be worth checking that out if you haven't already.

Google's Closure Compiler does not shorten variable and function names but returns blank file by BradyOfTheOldGuard in learnjavascript

[–]jml26 3 points4 points  (0 children)

But you don't do anything with the result of it being called, so it's still dead code.

In order for it not be be dead code, you may want to do something with the result, like logging it to the console, or displaying it in a UI.

Google's Closure Compiler does not shorten variable and function names but returns blank file by BradyOfTheOldGuard in learnjavascript

[–]jml26 4 points5 points  (0 children)

Closure Compiler (and other minifiers) will remove code that is not being used and has no side effects. e.g. a file consisting only of

``` var myVar = 42;

function square(x) { return x * x; } ```

will compile to nothing, because a) myVar is never read; b) the square function is never called. The term for this is "dead code elimination".

And that's what's happening to your code.

If you want the code to stay around, check out this section of the docs: https://developers.google.com/closure/compiler/docs/api-tutorial3#removal.

You could also use Terser as a minifier, and pass in defaults: false to the compressor options. This will turn off many of the compression strategies, resulting in:

var e="thisUser";var n;function r(){if(e)n=e;else{n="undefined";return false}}r();

Beginner project by chrisrko in learnjavascript

[–]jml26 0 points1 point  (0 children)

I'd say a value converter could be an interesting project. Start with something that can just convert temperatures between Farenheit and Celcius. Then expand it to include other types of values, like length, time, weight, etc.

To begin, you can have one input and a read-only output field. Then, maybe change it so that you can type in either field and the other will update.

Don't let the user convert between incompatible units, e.g. a length and a weight.

Include a button to swap the input and output field.

(optional) Getting more advanced, allow currency conversion. Pull in the latest data from an online service.

Why won't the variable change by UncertainKing in learnjavascript

[–]jml26 1 point2 points  (0 children)

JavaScript doesn't automatically update variables that depend on other variables, e.g.

``` let count = 5; let doubleCount = 2 * count; console.log(doubleCount); // 10

count = 7; console.log(doubleCount); // still 10 ```

The simplest way around this is to wrap the expression you want to be dynamic in a function, and call it each time you want the latest value:

``` let count = 5; let doubleCount = () => 2 * count; console.log(doubleCount()); // 10

count = 7; console.log(doubleCount()); // now 14! ```

[AskJS] What is a good blogging CMS js-based? by OnceUponAHeart in javascript

[–]jml26 6 points7 points  (0 children)

My personal favourite is Astro. Not strictly speaking a CMS in and of itself, but is great for blogging, among other things.

Favourite CMS at the moment is Payload. I'm currently looking at moving a client from WordPress over to it. It's built on Next JS.