Online JavaScript Compiler
About JavaScript
JavaScript runs in every browser and, through Node.js, on servers. Here your code runs on Node.js, so console.log prints to the output and input comes from stdin.
How to take input in JavaScript
Node.js reads standard input as one block. Read it with fs.readFileSync(0), then split it into lines and numbers.
const input = require("fs").readFileSync(0, "utf8").trim();
const lines = input.split("\n");
const n = Number(lines[0]);
const arr = lines[1].split(" ").map(Number);JavaScript programs to try
Copy one into the editor above, put any input in the Input tab and press Run.
Reverse a string
const s = require("fs").readFileSync(0, "utf8").trim();
console.log(s.split("").reverse().join(""));Input: techsnap
Count vowels
const s = require("fs").readFileSync(0, "utf8").toLowerCase();
const count = [...s].filter((c) => "aeiou".includes(c)).length;
console.log(count);Input: Online JavaScript compiler
Filter and map an array
const nums = require("fs").readFileSync(0, "utf8").trim().split(" ").map(Number);
const evensSquared = nums.filter((n) => n % 2 === 0).map((n) => n * n);
console.log(evensSquared);Input: 1 2 3 4 5 6
Common JavaScript errors and how to fix them
- TypeError: Cannot read properties of undefined
- You used a value that doesn't exist yet, often lines[1] when the input has only one line. Check the Input tab.
- ReferenceError: x is not defined
- The name is misspelled or used before it's declared with let, const or function.
- NaN in the output
- A string was used in arithmetic. Convert input with Number() or parseInt() first.
- SyntaxError: Unexpected token
- A bracket, quote or comma is missing or extra near the line in the message.
Frequently asked questions
Is this online JavaScript compiler free?
Yes. Write and run JavaScript code in your browser for free, with nothing to install and no setup.
How do I give input to my JavaScript program?
Type it in the Input tab before pressing Run and read it with require("fs").readFileSync(0, "utf8"), then split it into lines.
Does it run in the browser or Node.js?
Node.js. console.log works, but browser-only things such as document and window don't.
Does it work on my phone?
Yes. The editor, input and output work in a mobile browser, so you can practise JavaScript without a laptop.