Skip to content

Piero Bosio Social Web Site Personale Logo Fediverso

Social Forum federato con il resto del mondo. Non contano le istanze, contano le persone

Helo #JavaScript and TypeScript programmers.

Uncategorized
1 1 3
  • Helo and TypeScript programmers. (To which I don't count myself, even if I have to use it a bit now and then.)

    This is an anti-pattern, right?

    if (foo.indexOf("bar") === 0) {
    ...
    }

    Because if foo does *not* start with "bar", indexOf() will still search through all of foo for "bar". Even if you are only interested in seeing whether foo *starts* with "bar".

    Or are JavaScript interpeters and JITters clever enough to recognise this and silently turn it into effectively a use of startsWith() anyway? I suspect so. But still, nicer to write it optimally (and more obviously) from the start, right? Like this:

    if (foo.startsWith("bar") {
    ...
    }

  • Oblomovundefined Oblomov shared this topic on

Gli ultimi otto messaggi ricevuti dalla Federazione
Post suggeriti