💻 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:

    3 * (48 / 64)
    Run this program
  • Array Operations

    Work with an array of numbers:

    [1, 2, 3].map(x => x * x)
    Run this program
  • Variables and Calculations

    Define a variable and calculate the area of a circle:

    let radius = 5;
    Math.PI * Math.pow(radius, 2)
    Run this program
  • Functions

    You can also define your own functions (you can do it with the function keyword as well):

    force = (mass, velocity) => mass * velocity
    result = force(10, 2)
    Run this program
  • For Loops

    You can use for loops:

    numbers = [1, 2, 3, 4, 5]
    evens = []
    for (let number in numbers) {
        if (number % 2 == 0) {
            evens.push(Number(number))
        }
    }
    evens
    Run this program
  • Fetch Data

    You can fetch and display data from any public API, and you can use await when dealing with fetch:

    result = await fetch('https://jsonplaceholder.typicode.com/todos/1').then(res => res.json())
    result.title
    Run this program
  • Import Any JavaScript Library

    You can use JavaScript's await import(...) to make use of any third-party library, for e.g. lodash!

    const lodash = await import("https://esm.sh/lodash-es");
    const numbers = [1, 2, 3, 4];
    lodash.sum(numbers)
    Run this program

📘 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)

Useful Links