This online JavaScript compiler is a quick place to run JavaScript and see what it prints. Type code in the editor, press Run, and read the output in the console panel next to it. It is useful for homework, checking an idea, or practising for interviews.
How to run JavaScript online
- Edit the starter code, or click an Example such as Loops or Promises and async.
- Press Run, or Ctrl + Enter.
- Read the Console output. Warnings show in orange and errors in red.
- Fix any error using the line number shown, then run again.
- Click Copy output to copy what was printed, or Reset to go back to the starter code.
Each run starts fresh, so variables from the last run are gone.
What the console shows
The console prints values in a readable form, like browser developer tools:
const students = [
{ name: "Ravi", marks: 82 },
{ name: "Sneha", marks: 91 },
{ name: "Arjun", marks: 67 },
];
const toppers = students.filter((s) => s.marks >= 80);
console.log("Toppers:", toppers.map((s) => s.name));
const average = students.reduce((sum, s) => sum + s.marks, 0) / students.length;
console.log("Average marks:", average.toFixed(2));
Output:
Toppers: ["Ravi", "Sneha"]
Average marks: 80.00
These console methods work: console.log, console.info, console.warn, console.error, console.table (printed as text), console.count, console.time and console.timeEnd, console.assert and console.clear.
Reading error messages
When something goes wrong, the console shows the error type, message and line:
const marks = [70, 80];
console.log(mark.length);
Output:
ReferenceError: mark is not defined (line 2)
| Error | Usual cause |
|---|---|
| ReferenceError: x is not defined | Misspelt variable, or used before it was declared |
| TypeError: x is not a function | Calling something that is not a function, often a typo in a method name |
| TypeError: Cannot read properties of undefined | Reading a property of a value that is undefined |
| SyntaxError: Unexpected token | A missing bracket, comma or quote. Nothing runs until it is fixed |
Common mistake: A common mistake is sorting numbers with
sort()alone.[10, 9, 1].sort()gives[1, 10, 9], because sort compares values as text by default. Use[10, 9, 1].sort((a, b) => a - b)to get[1, 9, 10]. Try it with the Objects and arrays example.
JavaScript quirks to try in the console
| Code | Output | Why |
|---|---|---|
typeof null |
object | A long-standing bug kept for compatibility |
0.1 + 0.2 |
0.30000000000000004 | Decimal fractions are stored in binary |
"5" + 3 |
53 | + joins strings when one side is a string |
"5" - 3 |
2 | - always converts to numbers |
NaN === NaN |
false | NaN is not equal to anything, use Number.isNaN |
Curious why 0.1 + 0.2 is not exactly 0.3? See how numbers look in binary with the number system converter.
Safe by design
Your code runs in a hidden <iframe> with sandbox="allow-scripts". It can run JavaScript, but it cannot read this site's cookies or storage, open pop-ups or change this page. Nothing is sent to a server. Need HTML and CSS too? Use the online HTML, CSS and JavaScript editor, which adds a live page preview.
Learn as you practise
- Practise
map,filterandreducewith JavaScript array methods. - Run the code from JavaScript basics interview questions and promises and async/await interview questions to see the answers for yourself.
- Moving to the server side? Read the Node.js tutorial.
Pro tip: Print a label with every value, like
console.log("total:", total). When you have many log lines, labels tell you at a glance which value is which.
