Measures Concepts
GitHub icon

JavaScript

JavaScript - Programming language

< >

JavaScript is an open source programming language created in 1995 by Brendan Eich.

#2on PLDB 29Years Old 16mRepos

Try now: Web ยท Riju ยท Replit

JavaScript (), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. Read more on Wikipedia...


Example from Riju:
console.log("Hello, world!");
Example from hello-world:
console.log("Hello World");
// Hello world in JavaScript console.log("Hello World");
Example from Linguist:
alert("dude!")
Example from Wikipedia:
var minstake = 0.00000100; // valor base //----------------------------------------- var autorounds = 99; // nยฐ de rolls //====================================================== // if (profit > profit_max) { // error_title = "Maximum profit exceeded"; // error_info = "Maximum profit: " + number_format(profit_max, devise_decimal); // error_value = "Maximum profit exceeded - Maximum profit: " + number_format(profit_max, devise_decimal); // error = true; // } // else if (amount > balance) { // error_title = "Bet amount"; // error_info = "Maximum bet: " + number_format(balance, devise_decimal); // error_value = "Bet amount - Maximum bet: " + number_format(balance, devise_decimal); // error = true; // } var handbrake = 1.0000000; // valor lose pause game var autoruns = 1; // else if (amount > bet_max) { // error_title = "Bet amount"; // error_info = "Maximum bet: " + number_format(bet_max, devise_decimal); // error_value = "Bet amount - Maximum bet: " + number_format(bet_max, devise_decimal); // error = true; // } // else if (amount < bet_min) { // error_title = "Bet amount"; // error_info = "Minimum bet: " + number_format(bet_min, devise_decimal); // error_value = "Bet amount - Minimum bet: " + number_format(bet_min, devise_decimal); // error = true; // } function playnow() { if (autoruns > autorounds ) { console.log('Limit reached'); return; } document.getElementById('double_your_btc_bet_hi_button').click(); setTimeout(checkresults, 1000); return;} function checkresults() { if (document.getElementById('double_your_btc_bet_hi_button').disabled === true) { setTimeout(checkresults, 1000); return; } var stake = document.getElementById('double_your_btc_stake').value * 1; var won = document.getElementById('double_your_btc_bet_win').innerHTML; if (won.match(/(\d+\.\d+)/)ย !== null) { won = won.match(/(\d+\.\d+)/)[0]; } else { won = false; } var lost = document.getElementById('double_your_btc_bet_lose').innerHTML; if (lost.match(/(\d+\.\d+)/)ย !== null) { lost = lost.match(/(\d+\.\d+)/)[0]; } else { lost = false; } if (won &&ย !lost) { stake = minstake; console.log('Bet #' + autoruns + '/' + autorounds + ': Won ' + won + ' Stake: ' + stake.toFixed(8)); } if (lost &&ย !won) { stake = lost * 2.1; console.log('Bet #' + autoruns + '/' + autorounds + ': Lost ' + lost + ' Stake: ' + stake.toFixed(8)); } if (!won &&ย !lost) { console.log('Something went wrong'); return; } document.getElementById('double_your_btc_stake').value = stake.toFixed(8); autoruns++; if (stake >= handbrake) { document.getElementById('handbrakealert').play(); console.log('Handbrake triggered! Execute playnow() to override'); return; } setTimeout(playnow, 1000); return; }playnow()
The name Java in JavaScript was pure marketing: "At the time, the dot-com boom had begun and Java was the hot new language, so Eich considered the JavaScript name a marketing ploy by Netscape"
JavaScript Keywords
abstract arguments await boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with yield

Language features

Feature Supported Token Example
Scientific Notation โœ“
Binary Literals โœ“
// 0[bB][01]+n?
0b100110100000110011110010010
Floats โœ“
// (\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+)([eE][-+]?[0-9]+)?
80766866.0
Hexadecimals โœ“
// 0[xX][0-9a-fA-F]+n?
0x4D06792
Octals โœ“
// 0[oO]?[0-7]+n?
0o464063622
Sets โœ“
set = new Set()
set.add("foo")
Function Composition โœ“
function o(f, g) {
   return function(x) {
       return f(g(x));
   }
}
Destructuring โœ“
const o = {p: 42, q: true};
const {p, q} = o;
Default Parameters Pattern โœ“
function multiply(a, b = 1) {
 return a * b;
}
Line Comments โœ“ //
// A comment
Increment and decrement operators โœ“
let i = 0
i++
i--
Methods โœ“
class Person {
 method1() {}
 method2() {}
}
Functions โœ“
function helloWorld() {console.log("hi")}
Case Sensitivity โœ“
Zero-based numbering โœ“
While Loops โœ“
let times = 10
while (times) {times--}
console.log("done")
Ternary operators โœ“
let i = true ? 1 : 0
Switch Statements โœ“
var animal = "dog"
switch (animal) {
 case "dog": console.log("yay"); break;
 case "cat": console.log("oh"); break;
}
Strings โœ“ `
"hello world"
Letter-first Identifiers โœ“
Inheritance โœ“
class B {}
class A extends B {}
Print() Debugging โœ“ console.log
console.log("Hi")
References โœ“
Operators โœ“
1 + 1
Multiline Strings โœ“
const lines = `one
two`
Anonymous Functions โœ“
(() => console.log("hello world"))()
Infix Notation โœ“
const six = 2 + 2 + 2
Implicit Type Casting โœ“
console.log("hello " + 2)
Assignment โœ“ =
var name = "John"
Directives โœ“
"use strict";
"use asm";
Generators โœ“
function* fibonacci(limit) {
    let [prev, curr] = [0, 1];
    while (!limit || curr <= limit) {
        yield curr;
        [prev, curr] = [curr, prev + curr];
    }
}
// bounded by upper limit 10
for (let n of fibonacci(10)) {
    console.log(n);
}
// generator without an upper bound limit
for (let n of fibonacci()) {
    console.log(n);
    if (n > 10000) break;
}
// manually iterating
let fibGen = fibonacci();
console.log(fibGen.next().value); // 1
console.log(fibGen.next().value); // 1
console.log(fibGen.next().value); // 2
console.log(fibGen.next().value); // 3
console.log(fibGen.next().value); // 5
console.log(fibGen.next().value); // 8
// picks up from where you stopped
for (let n of fibGen) {
    console.log(n);
    if (n > 10000) break;
}
Garbage Collection โœ“
First-Class Functions โœ“
[2.0,1.1].map(Math.round)
Exceptions โœ“
try {
 undefinedFn()
} catch (err) {
 console.log(err)
}
hasDynamicTyping โœ“
Constants โœ“
const one = 1
Constructors โœ“
class Person {
 constructor(name) {
   this._name = name
 }
}
new Person("Jane")
Labels โœ“
main:
console.log("pldb")
Comments โœ“
Conditionals โœ“
if (true)
 console.log("hi!")
Classes โœ“
class Person {}
Method Chaining โœ“
"hello world".toString().substr(0, 1).length
Booleans โœ“ true false
Magic Getters and Setters โœ“
// Can be implemented in ES6 using proxies:
"use strict";
if (typeof Proxy == "undefined") {
    throw new Error("This browser doesn't support Proxy");
}
let original = {
    "foo": "bar"
};
let proxy = new Proxy(original, {
    get(target, name, receiver) {
        let rv = Reflect.get(target, name, receiver);
        if (typeof rv === "string") {
            rv = rv.toUpperCase();
        }
        return rv;
      }
});
console.log(`original.foo = ${original.foo}`); // "original.foo = bar"
console.log(`proxy.foo = ${proxy.foo}`);       // "proxy.foo = BAR"
Dynamic Properties โœ“
class Person {}
const person = new Person()
person.age = 50
Source Maps โœ“
{
 version: 3,
 file: 'min.js',
 names: ['bar', 'baz', 'n'],
 sources: ['one.js', 'two.js'],
 sourceRoot: 'http://example.com/www/js/',
 mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
};
Bitwise Operators โœ“
var x = 5 & 1;
Single Dispatch โœ“
Polymorphism โœ“
"a" + "b"; 1 + 2
MultiLine Comments โœ“ /* */
/* A comment
*/
Merges Whitespace โœ“
Lists โœ“
const list = [1,2,3]
Integers โœ“
80766866
Breakpoints โœ“
if (false)
  debugger
Partial Application โœ“
const addNumbers = (num1, num2) => num1 + num2
const add5 = num => addNumbers(10, num)
Map Functions โœ“
[1,2.1].map(Math.round)
Binary Operators โœ“
1 + 1
Async Await โœ“
async doSomething => await somethingElse()
Expressions โœ“
1 + 1
Regular Expression Syntax Sugar โœ“
console.log("Hello World".match(/\w/))
Statements โœ“
let x = 3;
File Imports โœ“
import { helloWorld } from "./helloWorld.js";
hasExports โœ“
export function myFunction() {
}
hasSymbols โœ“
// A symbol is a unique and immutable primitive value, often used as a unique key for object properties
pldb = Symbol()
Operator Overloading X
Case Insensitive Identifiers X
Multiple Inheritance X
Function Overloading X
Macros X
Processor Registers X
Multiple Dispatch X
Pointers X
Semantic Indentation X
Abstract Types X
Access Modifiers X
Variable Substitution Syntax X
Enums X
S-Expressions X

View source

- Build the next great programming language ยท Search ยท Add Language ยท Features ยท Creators ยท Resources ยท About ยท Blog ยท Acknowledgements ยท Queries ยท Stats ยท Sponsor ยท Day 605 ยท feedback@pldb.io ยท Logout