QuantScript — language-basics.qs
// ── Variables ────────────────────────────────────────────────────────────────
let x = 10;                 // mutable
const PI = 3.14159;         // immutable
var counter = 0;            // persistent across bars (trading-specific)

// ── Data Types ───────────────────────────────────────────────────────────────
let n = 42;                 // number
let s = "hello";            // string
let b = true;               // boolean
let nothing = null;          // null

// ── Strings ──────────────────────────────────────────────────────────────────
let full = "Quant" + "Script";              // concatenation
let up = "hello".toUpperCase();             // "HELLO"
let lo = "HELLO".toLowerCase();             // "hello"
let len = "hello".length;                   // 5
let has = "hello".includes("ell");          // true
let sub = "QuantScript".substring(0, 5);    // "Quant"
let parts = "a,b,c".split(",");             // ["a", "b", "c"]
let joined = ["x", "y"].join("-");           // "x-y"
let fmt = "Hi {0}, age {1}".format("Alice", 30);

// ── Arithmetic ───────────────────────────────────────────────────────────────
let sum = 10 + 3;     // 13
let diff = 10 - 3;    // 7
let prod = 10 * 3;    // 30
let quot = 10 / 3;    // 3.333...
let mod = 10 % 3;     // 1
let pow = 2 ** 8;     // 256

// ── Control Flow ─────────────────────────────────────────────────────────────
if (x > 100) {
    console.log("big");
} else if (x > 10) {
    console.log("medium");
} else {
    console.log("small");
}

let label = x > 10 ? "big" : "small";  // ternary

for (let i = 0; i < 10; i++) {
    if (i == 3) { continue; }
    if (i == 7) { break; }
}

let c = 5;
while (c > 0) { c = c - 1; }

let items = ["a", "b", "c"];
for (const item of items) {
    console.log(item);
}

// ── Arrays ───────────────────────────────────────────────────────────────────
let arr = [10, 20, 30];

let val = arr[0];                    // 10
let sz = arr.length;            // 3
arr.push(40);                 // append
let popped = arr.pop();         // remove & return last
let first = arr[0];
let last = arr[arr.length - 1];
let has30 = arr.includes(30); // true
let idx = arr.indexOf(30);
let slc = arr.slice(0, 2);    // [0..2)
let merged = arr.concat([50]);
arr.sort();                     // ascending
arr.sort((a, b) => b - a);  // descending
let total = arr.reduce((a, b) => a + b, 0);
let csv = arr.join(", ");
let zeros = [0, 0, 0, 0, 0];     // [0, 0, 0, 0, 0]

let [a, b2, c2] = [1, 2, 3];        // array destructuring

// ── Objects ──────────────────────────────────────────────────────────────────
let pt = { x: 10, y: 20 };
let px = pt.x;
let py = pt["y"];
pt.z = 30;              // add property dynamically

// ── Functions ────────────────────────────────────────────────────────────────
function add(a, b) {
    return a + b;
}

const multiply = (a, b) => a * b;
const square = (n) => n * n;

function greet(name, greeting = "Hello") {
    return greeting + ", " + name;
}

// named arguments
function config(host, port, secure) {
    return host + ":" + port;
}
let url = config("localhost", port: 8080, secure: true);

// ── Math ─────────────────────────────────────────────────────────────────────
Math.abs(-5); Math.max(1, 5, 3); Math.min(1, 5, 3);
Math.pow(2, 10); Math.sqrt(144); Math.round(3.7);
Math.ceil(3.2); Math.floor(3.9); Math.random(); Math.pi;

// ── Utilities ────────────────────────────────────────────────────────────────
null == null;           // true — use == null to check
null ?? 0;              // 0 — nullish coalescing
console.log("msg");
console.error("err");

← Back to all examples