r/javascript • • 21h ago

JavaScript - Wikipedia

Thumbnail en.wikipedia.org
0 Upvotes

JavaScript Turns 31

On December 4, 1995, Netscape Communications and Sun Microsystems issued a joint announcement of JavaScript.

The language itself was designed and implemented by Brendan Eich at Netscape in 10 days.

Happy Birthday!


r/javascript • • 35m ago

AskJS [AskJS] Reading the last line of a file when it has no trailing newline

0 Upvotes

A common mistake in Node.js is splitting file content by "\n" and assuming every line ends with a newline. If the last line does not, split("\n") still returns it as a final element, but code that iterates with readline() or relies on endsWith("\n") can silently drop or mishandle it.

The before:

const fs = require("fs");
const content = fs.readFileSync("data.txt", "utf-8");
const lines = content.split("\n");

for (const line of lines) {
  console.log(line);
}

If data.txt ends without a newline, the last line is still there, but any logic that expects a trailing newline on every element — or that filters out empty strings assuming they are artifacts — can behave inconsistently across files.

The after: trim only the final newline if present, then split:

const fs = require("fs");
let content = fs.readFileSync("data.txt", "utf-8");

if (content.endsWith("\n")) {
  content = content.slice(0, -1);
}

const lines = content.split("\n");

for (const line of lines) {
  console.log(line);
}

This works the same whether the file ends with \n, \r\n, or nothing at all. For \r\n handling, normalize first:

content = content.replace(/\r\n/g, "\n");

One edge case: an empty file returns [""] after splitting, which is usually not what you want. Guard with content.length === 0.

Question: Do you handle the no-trailing-newline case explicitly in your file-reading code, or do you assume inputs always end with a newline?


r/javascript • • 17h ago

I Made An Open-Source Cross-Platform Music Player

Thumbnail github.com
1 Upvotes

r/javascript • • 17h ago

Njectify - Customize any website with CSS and JavaScript

Thumbnail chromewebstore.google.com
0 Upvotes

Hello guys, hope it's okay to share my extension here!

I developed Njectify to give users more control over their browsing experience.

You can customize website layouts, remove annoying elements, automate repetitive tasks, add new functionality, and improve existing interfaces using custom JavaScript and CSS.

Everything is saved and automatically applied whenever you revisit a website.

It already has over 70 users, and I'd love to hear your feedback!

Available on the Chrome Web Store!

I really appreciate your feedbacks!


r/javascript • • 21h ago

SF Laser Taxi – a multiplayer waymo laser tag game

Thumbnail sflasertaxi.com
0 Upvotes

SF Laser Taxi is a multiplayer browser game where you drive a robotaxi around a small San Francisco and shoot lasers at the other players. There's no install and no account: pick a name and you're in.

Controls are WASD to drive, Space to fire and Shift to boost. Your score is how many taxis you've tagged since you last died, and the high score board keeps each name's best single life. There are 5 bots always driving around, so there's something to shoot at even when nobody else is online.

How it's built:

- The frontend is Babylon.js with plain ES modules, so there's no bundler and no build step. The textures, models and sky are CC0 assets from Poly Haven.

- The backend is one Node WebSocket server whose only dependency is ws. It sends position snapshots 20 times a second and owns HP, kills and deaths.

- Each client moves its own car and reports its own laser hits. The server rejects a hit if there was no recent shot, the target is too far away, or the target just respawned. That's enough for a game with no stakes, but it isn't cheat-proof.

- The city is generated from a seeded RNG, so every client gets the same buildings and laser collisions agree without the server ever sending a map.

- Phones get a lighter three.js renderer, because the HD scene was too heavy for mobile Safari. Both renderers use the same protocol, so phone and desktop players are in the same game.

The code is MIT licensed: https://github.com/bortpro/WaymoWorld

A tip: on desktop, type "starman" while you're driving. Your taxi turns into a flying roadster. Z and X fire the left and right thrusters, both together lift you, and you can land on rooftops.

I'd like to hear how the netcode feels on your connection, and what you'd add next.


r/javascript • • 10h ago

TTSC: TypeScript v7 Runtime (ts-node alternative) + unplugin for every bundlers

Thumbnail github.com
3 Upvotes

r/javascript • • 21h ago

Announcing TypeScript 7.1 RC

Thumbnail devblogs.microsoft.com
80 Upvotes