Spyke
lemmy.world

You'll find an npm package to help you count up to 2.

(I recently learned - maybe here - that the is-even package has over 170k weekly downloads)

97
Drusenijareply
lemmy.world

What's even wilder is if you look at the code of that package, all it does is include the is-odd package and then return !is-odd. And the is-odd package isn't much better, it does some basic checks on the input and then returns n % 2 === 1.

84
lemmy.world

I thought I was missing something. JS is one of my main languages and I always just write the is-odd function myself since it's like 10 characters. It boggles the mind that is-even has 176k weekly downloads

29
kevincoxreply
lemmy.ml

To be fair having a name can make things easier to read. I get that i % 2 == 0 is a common pattern and most programmers will quickly recognize what is happening. But isEven(i) is just that much easier to grok and leaves that brainpower to work on something else.

But I would never import a package for it. I would just create a local helper for something this trivial.

20
lemmy.world

Exactly what I would do if I had to reuse it, especially now since I know that adding a package would actually add 2. It all just seems so...inefficient

11
kevincoxreply
lemmy.ml

Even if the code isn't reused adding names to sub-expressions can be very valuable. Often times I introduce new functions or variables even if they are only used once so that I can give them a descriptive name which helps the reader more quickly understand what is happening.

11

Yeah, I do that with pretty much every separate operation in c# since our solutions are pretty big. Most of my JS scripts are just done in ServiceNow which are separated and named appropriately.

3

Also there are 40-something packages depending on it, so I guess it gets pulled automatically when they are used.

8

If youre lazy/busy enough, doing basic checks on the input is enough boilerplate to package out.

2

This must be a "hold my beer" kind of joke and someone wanting to see how far they can take it.

10
  1. it's easy to make fun of
  2. it makes every other programming language look better in comparison
70
  1. It runs in browsers
  2. If you hate your co-workers, then they will also feel your pain.
66
lemmy.ml
  1. You can call it "Java" to enrage other programmers
  2. You can compare numbers against strings without wasting time converting them
60

JavaScript: :wide eyed and smiling: Sure why not! You're the boss!

Python: Sighing and downing half a bottle of Advil: Sure. Why not, you're the boss.

5
TrickDacyreply
lemmy.world

If you're living in 2002 and not using the strict equality operator, that's on you

7
lemmy.zip

But what if I don't want strict comparison? What if my frontend contains a text field for a numeric input and I wanna manually check against each possible valid input value if (input_val == 1) {...} else if (input_val == 2) {...} else if... without having to convert it first or check that it's actually a number or fix my frontend?

(I'm sure there are valid use cases for non-strict comparison, I just can't think of one right now)

1
feddit.uk

why wouldn't you just convert inline? (Number(input_val) === 2)

Converting would mean you could use a switch statement with your example rather than an if else ladder

switch(Number(input_val)) {
  case 1:
    doTheFirstThing();
  break;
  case 2:
    doTheSecondThing();
  break;
  case 3:
    doTheThirdThing();
  break;
}
4

If you're looking for good code, you missed the point of my comment 😄

If I was looking for an enumeration of valid inputs, I'd make it a selection box rather than a text field that's supposed to contain a number and give the selections reasonable names. If I want an integral quantity, I'd use a number input field.

If I have no control over the frontend, that means I'm writing a backend in JS for a bullshit frontend, and no amount of good coding practice is going to salvage this mess.

I'm also blessedly far away from WebDev now. I work in Data Analytics and if I ever have to do any of this for a living, something has gone very wrong.

Converting texts into numbers or dates still haunts me though - fuck text inputs for numbers and dates.

-1
sh.itjust.works

If you are understood buy you're audience, you have spoken correctly. Correcting someone's grammer is pointless

-8

I dunno what correcting someone's grammar has to do with paying off your readers. Sounds expensive.

5

This isn’t speaking, but writing (or typing). Using ‘correct’ spelling & grammar helps ESL speakers read the language as well as those relying on text translation software. Some folks make typos & it’s fine to make mistakes but it’s also strange to act like it’s just as easy to understand. Apostrophes have a specific meaning & many folks rely on them for understanding.

I’m learning a foreign language now & I can tell you it is a massive stumbling block when you run into what you think is a new word, but is ‘just’ a misspelling.

My issue with this account is not its corrections, but if you want to be the correction bot, at least get the typography right too. ' is as ASCII holdover & it should be .

3
Hexareireply
programming.dev

PHP has gotten really good over the past few versions, actually. Lots of really great stuff has been added, it feels like it resembles rust more every release lol

18
lemmy.ml

PHP10: We now allow interop with Rust!

PHP11: We now allow writing code directly in a .php file and compile it with rustc.

1
lemmy.world
  1. It has the simplest multi-threaded implementation: no threads!
  2. You can goof off while your build system takes 20 minutes to rebuild a non-compiled language
57
lemmy.world

I am forced to try to get a JS certification.

I am reaching the end of my rope, and starting to think of maybe putting my neck into one.

Isaac Newton said that we see far because we stand on the shoulders of giants.

Javascript is like standing on the shoulders of dwarves with brittle bone disease.

41

Standing on the shoulder of dwarves hiding deep underground

5
lemmy.ml

1 - Easiest way to run a script in your browser
2 - Always finds its way if inputs are bad
Nan - undefined

40

To be fair, this is actually reasonable. But it does look stupid on the face of it.

11
joneskindreply
lemmy.world

When my console throws a NaN I kinda think of it as an Halloween kid receiving a fruit instead of a candy. They won’t say “That’s a fruit”. They’ll say “That’s not a treat”.

I’m personally pissed more often by a falsy 0.

Did you know that early analog computers would literally explode when asked to divide by 0?

Now computers just say “Hey stupid, that shit is not even a Number in a mathematical sense, but sure I’ll add one to it.” instead of “Why would you kill me like this?”

You can’t really define Infinity as a number, yet it is part of their world.

So typeof NaN === ‘number’ totally makes sense in that regard.

If you ever worked with arrays of dates, don’t judge NaN too harshly.

1
Skullgridreply
lemmy.world

Falsy zero? What's wrong with that, 1 is true and 0 is false. I thought that was standard logic?

1
joneskindreply
lemmy.world

in javascript a property is truthy if it exists

myThing.property = "some string"

if (myThing.property) { // true
  // do something
}

It works with everything except of course for falsy values

myThing.number = someNumberThatShouldNotBeEqualToZero

if (myThing.number) {
  // do something very important with that number that should not be equal to zero
}

// This can fail at anytime without warning

So you've got to be extra careful with that logic when you're dealing with numbers.

I am not saying it's wrong though. I'm saying it's often annoying.

1
Skullgridreply
lemmy.world

ah ok , I think I write this a bit more verbose when using other languages, instead of

if(thing)
{
   stuff;
}

I do


if(thing != null)
{
   stuff;
}

so checking for numbers being truthy & existing didn't seem like an issue

1

In the case of a non-existing property, the value would be undefined rather than null.

And while == and != exist in JavaScript, most linters will throw an error and require a === and !== instead as they should be avoided.

null == undefined // true
null === undefined // false

Besides, null is a perfectly valid value for a property, just as 0. Working with API Platform, I couldn't tell the number of times I used this kind of statement:

if (property || property === null) {
  // do some stuff
}

Probably just as much as

if (property || property === 0) {
  // do some stuff
}
1
  1. Ubiquitous; insane amount of libraries and probably some of the best documentation of any language
  2. JS lambda function syntax is nice
38
feddit.uk

It leads to typescript

You get surprises from npm

32
ByteJunkreply
lemmy.world

I spent way too long today figuring out why my app was doing something that it's NOT supposed to do on weekends.

I read Luxon's docs (pretty cool lib tbh) again and again, and tried everything I could think of to get isWeekend to return a sane result.

Turns out I was pulling a somewhat older version of Luxon, where isWeekend didn't exist. In any sane language, I expect I'd get a huge warning about a property that doesn't exist, but alas...

Typescript helps me keep my sanity, but juuuuust barely.

28

If isWeekend doesn't exist, then the weekend doesn't exist, so it's naturally false.

That's why JavaScript gets pushed so hard - it's part of the capitalist agenda to keep us working 7 days a week

20

That's fair. Typescript has to cook with the existing js ecosystem.

7
kamenreply
lemmy.world

Weren't you getting runtime errors for the function not being found?

5
shastaxcreply
lemm.ee

Falsy* because it was undefined

However, their IDE should have highlighted it as an unknown property. Guess this guy is coding in notepad or vi.

1
lemmy.dbzer0.com

Yep, thanks for correcting me. In fact, if they write something like

if (day.isWeekend) {...}

The block will never be executed with the old version of library

2
shastaxcreply
lemm.ee

Yeah that's exactly what I think happened to him. He needs a better IDE and/or needs to stop copy/pasting code from stackoverflow or documentation that doesn't match his library version.

2

My dude, you need to understand that all that anger and resentment, it is not you. It's the years of JavaScript poisoning your mind.

In any case, that goes to my point. I would have to be saved by my IDE, when any sane language will blow up in your face as soon as you try to run it.

0
owseireply
programming.dev

I don't know how luxon works, but isWeekend could be a property instead of a function

2

It is. It also happens to be undefined, and checking that for truth is how I was bitten.

2
lemmy.ml
  1. Is available to be used in website scripting.
  2. Not quite as full of footguns as PHP (although it is close).
21
thelemmy.club

The bot toggle was on for my account for some reason but I am human... I think

It should be off now

4

It seems it doesn't propagate to other servers immediately though.

4

They're not I'm sure, they just wanna bitch about a language without caring about why it is how it is.

2
lemmy.world

It runs in browsers. It… isn’t poop? I don’t know. I’m all out of ideas.

13

We have forced it, quite hamfistedly, to do anything. The organic hell-evolution of web browsers turned them into do-anything sandboxed mini-OS. It meant whatever hellish code you used to write your corporate mandated web app could now become a perfectly bloated standalone application. And the demonic language that would enable it was called Javascript. It does the backend and it does the frontend. You could consider those advantages over other devices, like toasters and those handheld electronic games from the 80s.

12
  1. It at least feels slightly little bit like lisp.
  2. Shit I'm f*ed

.. ah right, it runs on any browser. Lame

11

Everyone know JavaSript is a Java, but you don't have to compile, so you script in it.

^/s^

10

Browsers love it!

Practically anything you write will execute without all that scope and well formed statements nonsense.

Mind you, number 2 is also its biggest flaw as well, but…

9

hmm, let's see.

It's not java.

It's also not a scripting language.

also to the repeat grammar nazi in the comments here, hi, "its"

9
jvereply
lemmy.world

It’s not a scripting language?

27
kbin.social

Depends on how you define "scripting language".

Older techs remember when it was only browser-based and they thought of, and perhaps still think of, "scripting languages" as something that would run from some command-line or another. Starting a GUI browser to run a mere script was a ridiculous concept. (There was also that JavaScript had no filesystem access. At least initially. And then it became a gaping security hole, but I digress.)

Today, there exist command-line accessible versions of JavaScript but even there (I figure) most people wince and choose anything else instead. Maybe even Perl.

But another definition of "scripting language" is "(any) interpreted programming language" and where it runs is unimportant.

From that perspective, sure, JavaScript qualifies. And so does QBASIC.

4
shastaxcreply
lemm.ee

A script is just a file that can execute a series of commands without the need to compile

7

They compile in some point of time because CPU don't know shit about Javascript. But that is for some other discussion.

Edit: typo

2

Are you referring to AOT compilation specifically? JavaScript in V8 is JIT compiled if it's "hot" (executed enough that the cost of JIT compilation is less than the cost of continuing to run it in interpreted mode).

1

Hot take: A scripting language is a programming language whose execution starts from the beginning of the file | stream.

1
lemmy.dbzer0.com

bash would be a scripting language, though to be fair, i also consider bash to be pseudo code as well.

If JS is a scripting language, than any other language is a scripting language. And technically, every language can be used to script, so therefore, is a scripting language. i'm referring to the aspect of a scripting language being generally constricted.

1
meteokrreply

Pseudo code is literally fake code. Scripting is an actual type of code. Scripted languages while not strictly defined, usually refers to languages you don't compile before running them. Bash is considered a scripting language because you don't ship a binary compiled executable, but rather ship a file that is human readable and converted into machine code when it is run. Scripting languages are compared to compiled languages, like C or Rust. Where the file you run is already compiled, and executed directly.

What do you mean by this?

i’m referring to the aspect of a scripting language being generally constricted.

Any Turing complete system, or this case language, can do anything any other one can, depending on the level of suffering you are willing to endure to make it happen. Anything JS can do, Rust can do. Anything Rust can do, Bash can do. The differences between languages is the assumptions they make, and performance characteristics as a result of those assumptions. Functionality is not practically different from one another, though some absolutely make it easier for humans to do.

3
lemmy.dbzer0.com

Pseudo code is literally fake code.

hence why i specified why i consider it to be as such. I just think pseudo code shouldn't exist. Plain and simple. Bash scripting is close to a language in the same way that pseudo code is also technically code.

What do you mean by this?

i just mean the simple fact that you could technically probably run bash on windows, but really wouldn't want to. I don't consider bash to be a programming language, though it is technically a scripting language, because it's primary existence is in the shell environment of a system. I.E. constricted, but that's just my view of it.

0

Bash being on the same level as actually fake code is a pretty hot take to me. What are your opinions on Python, or Ruby, or any other interpreted language? You could very well use them as your login shell, just like Bash if you wanted. In your eyes, if Bash *isn't * a programming language at all, how do you describe a programming language? Languages that express code are just the same as languages that write stories, and whether you do it in German or Vietnamese makes no difference on what story you can write.

When you describe a language as constricted what do you mean? Bash can do anything Python or Rust can do, each of them is just specialized to being better at specific aspects for human convenience in writing code. There is no inherit limitation on what can be done by the language you use to express it.

3

it's definitely a hot take, you could theoretically use any other language as a shell, and i know you sort of can with stuff like node as well.

But those aren't shell languages, and bash isn't a true language, in the sense that it was explicitly designed to be used in the shell environment, i also consider it to be "pseudocode" because it's not actually bash doing things a pretty significant amount of time you'll stuff something into sed or awk, which are actually different interpreters all together.

bash is almost a sort of wrapper, between a bunch of different programs that all handle things differently, allowing you to glue them back together to make something usable. It's close enough to being it's own language, that you could make it one, but it's not, because it's not supposed to be one.

i suppose that's pretty much what i mean when i say constricted.

1
inetknghtreply
lemmy.ml

It’s also not a scripting language.

It definitely is a scripting language.

hello-world.js:

#!/usr/bin/env node

console.log("Hello world");

Your favorite command line tool:

chmod +x ./hello-world.js
./hello-world.js

You just need to install npm, eg via apt-get install npm.

5
lemmy.ml

Damn this is hard. I keep java script disabled by default so its hard to say anything good. I begrudgingly have to whitelist websites i need that cant run without it.

8
ADTJreply
feddit.uk

I feel like that's more because you don't want websites you visit running code than because you dislike this particular programming language

2

Thats true. It is more to do with code execution than the language itself.

1
kbin.social

You can make your speakers go BRRRRRRRRR via Home Assistant with it

3