💻 Code Calculator auto-saving as you type
0/2000 characters
took 0ms
💡 What is this tool?
This tool allows you to write and execute JavaScript code directly in your browser. There is nothing storing your code except the URL of this page itself, which is updated as you type your code. It's useful for doing multi-line calculations, developling and testing code snippets, or even sharing small scripts by simply sharing the URL. Results are updated in real-time as you evaluate the code.
📘 Getting Started Examples
Simple Arithmetic
Calculate the sum of two numbers:
Run this program3 * (48 / 64)
Array Operations
Work with an array of numbers:
Run this program[1, 2, 3].map(x => x * x)
Variables and Calculations
Define a variable and calculate the area of a circle:
Run this programlet radius = 5; Math.PI * Math.pow(radius, 2)
Functions
You can also define your own functions (you can do it with the
function
keyword as well):
Run this programforce = (mass, velocity) => mass * velocity result = force(10, 2)
For Loops
You can use for loops:
Run this programnumbers = [1, 2, 3, 4, 5] evens = [] for (let number in numbers) { if (number % 2 == 0) { evens.push(Number(number)) } } evens
Fetch Data
You can fetch and display data from any public API, and you can use
await
when dealing withfetch
:
Run this programresult = await fetch('https://jsonplaceholder.typicode.com/todos/1').then(res => res.json()) result.title
Import Any JavaScript Library
You can use JavaScript's
await import(...)
to make use of any third-party library, for e.g. lodash!
Run this programconst lodash = await import("https://esm.sh/lodash-es"); const numbers = [1, 2, 3, 4]; lodash.sum(numbers)
📘 Reference
General JavaScript
- Print to console:
console.log('Hello, world!')
- Show alerts and prompts:
alert('Message'), prompt('Your name?')
- Handle errors:
try { ... } catch(e) { console.error(e) }</code>
- Work with dates:
new Date(), Date.now()
- Set a timeout or interval:
setTimeout(() => alert('Done!'), 1000)
Browser-Specific APIs
- Fetch data:
await fetch('https://jsonplaceholder.typicode.com/todos/1').then(res => res.json())
- Manipulate the DOM:
document.querySelector('h1').innerText = 'Updated!'
- Interact with localStorage:
localStorage.setItem('key', 'value')
- Navigate to a URL:
window.location.href = 'https://example.com'
Math
- Basic operations:
2 + 3, 10 - 4, 5 * 3, 20 / 4
- Exponents:
Math.pow(3, 4), 3 ** 4
- Square root:
Math.sqrt(16)
- Random numbers:
Math.random()
- Trigonometry:
Math.sin(Math.PI / 2), Math.cos(0)
Strings
- Change case:
'hello'.toUpperCase(), 'WORLD'.toLowerCase()
- Concatenate strings:
'hello' + ' world'
- Repeat:
'abc'.repeat(3)
- Extract substrings:
'hello'.substring(0, 2)
- Find patterns:
'abc123'.match(/\d+/)
Arrays
- Map:
[1, 2, 3].map(x => x * x)
- Filter:
[1, 2, 3].filter(x => x > 1)
- Reduce:
[1, 2, 3].reduce((a, b) => a + b, 0)
- Sort:
[3, 1, 2].sort()
- Find:
[1, 2, 3].find(x => x === 2)