JavaScript · 642 B
Sample JavaScript file
An idiomatic JavaScript example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.
CC0 · free for any use · Verified by an independent tool · Report a problem
// Sample JavaScript module — samplefiles.com
export class Customer {
constructor(id, name, balance) {
this.id = id;
this.name = name;
this.balance = balance;
}
get isVip() {
return this.balance >= 10_000;
}
}
export const totalBalance = (customers) => customers.reduce((sum, c) => sum + c.balance, 0);
const customers = [new Customer(1, 'Ada', 12_500), new Customer(2, 'Grace', 830.5), new Customer(3, 'Linus', 10_000)];
for (const c of customers) {
console.log(`${c.name.padEnd(6)} ${c.balance.toFixed(2).padStart(10)} ${c.isVip ? 'VIP' : ''}`);
}
console.log(`Total: ${totalBalance(customers).toFixed(2)}`);File details
Download from the command line
curl -L -o sample.js https://files.samplefiles.com/code/js/sample.jswget https://files.samplefiles.com/code/js/sample.jshttps://files.samplefiles.com/code/js/sample.jsprintf '%s %s\n' 79581c927fbdac71a1c4fe99ab489236b4e1808dd01a0a94bc01bc6a9eb7221a sample.js | shasum -a 256 -cOr check the download in your browser with the checksum calculator.
Same sample in other formats
Python · 620 BTypeScript · 733 BJSX · 596 BTSX · 788 BJava · 763 BKotlin · 541 BScala · 519 BC# · 689 BGo · 631 BRust · 771 BC · 783 BC header · 353 BC++ · 925 BSwift · 665 BDart · 639 BRuby · 461 BPHP · 749 BPerl · 524 BLua · 470 BR · 417 BJulia · 479 BHaskell · 578 BElixir · 675 BErlang · 719 BClojure · 488 BF# · 508 BZig · 762 BObjective-C · 861 BBash · 425 BPowerShell · 484 BAssembly · 610 BCSS · 645 BSCSS · 508 BVue · 611 BSvelte · 397 BDockerfile · 342 BMakefile · 346 BJupyter Notebook · 1.9 KBGroovy · 501 BFortran · 843 BPascal · 1 KBOCaml · 608 BNim · 616 BSolidity · 1.2 KBVerilog · 986 BVHDL · 920 BCommon Lisp · 751 BScheme · 747 BProlog · 574 BAda · 749 BD · 553 BElm · 985 BGleam · 697 BTcl · 414 BCoffeeScript · 475 BLess · 488 BSass · 510 BCMake · 692 BWebAssembly text (WAT) · 304 BWASM · 71 BJava class · 2.1 KBPYC · 1.2 KBSource map · 1.3 KB
Similar JavaScript files
| File | Size | Details | Download |
|---|---|---|---|
| Minified JavaScript (with source map)sample.min.js | 443 B | Download | |
| Large JavaScript file (about 10,000 lines)sample-10000-lines.js | 205 KB | 9,998 lines | Download |