Skip to content

How to Use the Expression Calculator

The expression parser is aimed at a mathematical audience, not a programming audience. The syntax is similar to most calculators and mathematical applications. Key differences include:

  • Matrix indexes are one-based instead of zero-based
  • There are index and range operators which allow more conveniently getting and setting matrix indexes, like A[2:4, 1]
  • Both indexes and ranges have the upper-bound included
  • There is a differing syntax for defining functions. Example: f(x) = x^2
  • There are custom operators like x + y instead of add(x, y)
  • Some operators are different. For example ^ is used for exponentiation, not bitwise xor
  • Implicit multiplication, like 2 pi, is supported and has special rules
  • Relational operators (<, >, <=, >=, ==, and !=) are chained, so 5 < x < 10 is equivalent to 5 < x and x < 10
  • The precedence of some operators is different

The expression calculator uses conventional infix notation for operators: an operator is placed between its arguments. Round parentheses can be used to override the default precedence of operators.

Examples:

Basic Operators
2 + 3 # Result: 5
2 * 3 # Result: 6
# use parentheses to override the default precedence
2 + 3 * 4 # Result: 14
(2 + 3) * 4 # Result: 20
OperatorDescriptionExampleResult
(, )Grouping(x)-
[, ]Matrix, Index[[1,2],[3,4]]Matrix
{, }Object{a: 1, b: 2}Object
,Parameter separatormax(2, 1, 5)5
.Property accessorobj.prop-
;Statement separatora=2; b=3; a*b[6]
\nStatement separatora=2 \n b=3[2,3]
+Add4 + 59
+Unary plus+44
-Subtract7 - 34
-Unary minus-4-4
*Multiply2 * 36
.*Element-wise multiply[1,2,3] .* [1,2,3][1,4,9]
/Divide6 / 23
./Element-wise divide[9,6,4] ./ [3,2,2][3,3,2]
%Percentage8%0.08
%Addition with Percentage100 + 3%103
%Subtraction with Percentage100 - 3%97
% modModulus8 % 32
^Power2 ^ 38
.^Element-wise power[2,3] .^ [3,3][8,27]
'Transpose[[1,2],[3,4]]'[[1,3],[2,4]]
!Factorial5!120
&Bitwise and5 & 31
~Bitwise not~2-3
|Bitwise or5 | 37
^|Bitwise xor5 ^| 27
<<Left shift4 << 18
>>Right arithmetic shift8 >> 14
>>>Right logical shift-8 >>> 12147483644
andLogical andtrue and falsefalse
notLogical notnot truefalse
orLogical ortrue or falsetrue
xorLogical xortrue xor truefalse
=Assignmenta = 55
? :Conditional15 > 100 ? 1 : -1-1
??Nullish coalescingnull ?? 22
?.Optional chaining accessorobj?.prop-
:Range1:4[1,2,3,4]
to, inUnit conversion2 inch to cm5.08 cm
==Equal2 == 4 - 2true
!=Unequal2 != 3true
<Smaller2 < 3true
>Larger2 > 3false
<=Smaller or equal4 <= 3false
>=Larger or equal2 + 4 >= 6true

From highest to lowest precedence:

  1. (...) [...] {...} - Grouping, Matrix, Object
  2. x(...) x[...] obj.prop : - Function call, Matrix index, Property accessor
  3. ' - Matrix transpose
  4. ! - Factorial
  5. ?? - Nullish coalescing
  6. ^, .^ - Exponentiation
  7. +, -, ~, not - Unary operators
  8. % - Unary percentage
  9. Implicit multiplication
  10. *, /, .*, ./, %, mod - Multiply, divide, modulus
  11. +, - - Add, subtract
  12. : - Range
  13. to, in - Unit conversion
  14. <<, >>, >>> - Bitwise shifts
  15. ==, !=, <, >, <=, >= - Relational
  16. & - Bitwise and
  17. ^| - Bitwise xor
  18. | - Bitwise or
  19. and - Logical and
  20. xor - Logical xor
  21. or - Logical or
  22. ?, : - Conditional expression
  23. = - Assignment
  24. , - Parameter separator
  25. ; - Row separator
  26. \n, ; - Statement separators

Lazy Evaluation:

Bitwise and logical operators use lazy evaluation when possible:

false and x # Result: false (x is not evaluated)

Functions are called by entering their name, followed by zero or more arguments enclosed by parentheses.

Examples:

sqrt(25) # Result: 5
log(10000, 10) # Result: 4
sin(pi / 4) # Result: 0.7071067811865475

You can define custom functions by “assigning” an expression to a function call:

Examples:

f(x) = x ^ 2 - 5
f(2) # Result: -1
f(3) # Result: 4
g(x, y) = x ^ y
g(2, 3) # Result: 8

Note on Dynamic Variables: Functions do not create closures - all variables are dynamic:

x = 7
h(y) = x + y
h(3) # Result: 10
x = 3
h(3) # Result: 6 (not 10!)

Passing Functions as Parameters:

twice(func, x) = func(func(x))
square(x) = x ^ 2
twice(square, 2) # Result: 16
f(x) = 3*x
twice(f, 2) # Result: 18

Most operators have function equivalents:

OperatorFunctionExample
a + badd(a, b)add(2, 3) = 5
a - bsubtract(a, b)subtract(7, 3) = 4
a * bmultiply(a, b)multiply(2, 3) = 6
a / bdivide(a, b)divide(6, 2) = 3
a ^ bpow(a, b)pow(2, 3) = 8
a % bmod(a, b)mod(8, 3) = 2
a == bequal(a, b)equal(2, 2) = true
a and band(a, b)and(true, false) = false
not anot(a)not(true) = false

Some commonly used functions include:

  • Trigonometric: sin(), cos(), tan(), asin(), acos(), atan()
  • Exponential: exp(), log(), log10(), sqrt()
  • Rounding: round(), floor(), ceil(), fix()
  • Statistical: min(), max(), mean(), median(), std()
  • Matrix: det(), inv(), transpose(), dot(), cross()

Some data types have methods which can be accessed using dot notation:

Unit Methods:

a = 1 m
a.toNumber("mm") # Result: 1000

Matrix/Array Methods:

M = [4, 9, 25]
M.map(sqrt) # Result: [2, 3, 5]

The map function applies a function to each element of an array:

square(x) = x ^ 2
map([1, 2, 3, 4], square) # Result: [1, 4, 9, 16]
map([1, 2, 3, 4], f(x) = x ^ 2) # Result: [1, 4, 9, 16]
map([1, 2, 3, 4], x ^ 2) # Result: [1, 4, 9, 16]

The calculator has the following built-in mathematical constants:

ConstantDescriptionValue
e, EEuler’s number, base of natural logarithm2.718281828459045
pi, PIRatio of circle’s circumference to diameter3.141592653589793
tauRatio of circle’s circumference to radius (2π)6.283185307179586
phiGolden ratio: (1 + √5) / 21.618033988749895
iImaginary unit: √-1i
InfinityPositive infinityInfinity
NaNNot a numberNaN
nullNull valuenull
undefinedUndefined valueundefined
LN2Natural logarithm of 20.6931471805599453
LN10Natural logarithm of 102.302585092994046
LOG2EBase-2 logarithm of e1.4426950408889634
LOG10EBase-10 logarithm of e0.4342944819032518
SQRT1_2Square root of 1/20.7071067811865476
SQRT2Square root of 21.4142135623730951

Examples:

pi # Result: 3.141592653589793
e ^ 2 # Result: 7.3890560989306495
log(e) # Result: 1
e ^ (pi * i) + 1 # Result: ~0 (Euler's identity)
tau # Result: 6.283185307179586 (2 * pi)
phi # Result: 1.618033988749895 (golden ratio)
sin(pi / 4) # Result: 0.7071067811865475
i * i # Result: -1
SQRT2 # Result: 1.4142135623730951
LN10 # Result: 2.302585092994046

Math.js includes numerous physical constants for scientific calculations:

ConstantSymbolValueUnit
speedOfLightc299792458m · s⁻¹
gravitationConstantG6.6738480e-11m³ · kg⁻¹ · s⁻²
planckConstanth6.626069311e-34J · s
reducedPlanckConstant1.05457172647e-34J · s
ConstantSymbolValueUnit
magneticConstantμ₀1.2566370614e-6N · A⁻²
electricConstantε₀8.854187817e-12F · m⁻¹
vacuumImpedanceZ₀376.730313461Ω
coulombκ8.9875517873681764e9N · m² · C⁻²
elementaryChargee1.60217656535e-19C
bohrMagnetonμB9.2740096820e-24J · T⁻¹
conductanceQuantumG₀7.748091734625e-5S
inverseConductanceQuantumG₀⁻¹12906.403721742Ω
magneticFluxQuantumφ₀2.06783375846e-15Wb
nuclearMagnetonμN5.0507835311e-27J · T⁻¹
klitzingRK25812.807443484Ω
ConstantSymbolValueUnit
bohrRadiusa₀5.291772109217e-11m
classicalElectronRadiusre2.817940326727e-15m
electronMassme9.1093829140e-31kg
fermiCouplingGF1.1663645e-5GeV⁻²
fineStructureα7.297352569824e-3-
hartreeEnergyEh4.3597443419e-18J
protonMassmp1.67262177774e-27kg
deuteronMassmd3.3435830926e-27kg
neutronMassmn1.6749271613e-27kg
quantumOfCirculationh/(2me)3.636947552024e-4m² · s⁻¹
rydbergR∞10973731.56853955m⁻¹
thomsonCrossSection-6.65245873413e-29
weakMixingAngle-0.222321-
efimovFactor-22.7-
ConstantSymbolValueUnit
atomicMassmu1.66053892173e-27kg
avogadroNA6.0221412927e23mol⁻¹
boltzmannk1.380648813e-23J · K⁻¹
faradayF96485.336521C · mol⁻¹
firstRadiationc₁3.7417715317e-16W · m²
loschmidtn₀2.686780524e25m⁻³
gasConstantR8.314462175J · K⁻¹ · mol⁻¹
molarPlanckConstantNA · h3.990312717628e-10J · s · mol⁻¹
molarVolumeVm2.241396820e-10m³ · mol⁻¹
sackurTetrode--1.164870823-
secondRadiationc₂1.438777013e-2m · K
stefanBoltzmannσ5.67037321e-8W · m⁻² · K⁻⁴
wienDisplacementb2.897772126e-3m · K
ConstantSymbolValueUnit
molarMassMu1e-3kg · mol⁻¹
molarMassC12M(¹²C)1.2e-2kg · mol⁻¹
gravitygn9.80665m · s⁻²
atmatm101325Pa
ConstantSymbolValueUnit
planckLengthlP1.61619997e-35m
planckMassmP2.1765113e-8kg
planckTimetP5.3910632e-44s
planckChargeqP1.87554595641e-18C
planckTemperatureTP1.41683385e+32K

Examples:

speedOfLight # Result: 299792458 m / s
avogadro * 12 g # Result: 6.0221412927e23 * 12 g
boltzmann * 300 K # Result: 4.141946439e-21 J
gravity * 70 kg # Result: 686.4655 N

Variables can be defined using the assignment operator =:

Examples:

a = 3.4 # Result: 3.4
b = 5 / 2 # Result: 2.5
a * b # Result: 8.5

Variable Naming Rules:

Variable names must:

  • Begin with a letter (a-z, A-Z), underscore (_), dollar sign ($), or Unicode letter
  • Contain only letters, digits (0-9), and allowed special characters
  • Not be reserved words: mod, to, in, and, xor, or, not, end

Valid examples: x, myVar, _temp, $result, α (Greek letters are allowed)

Numbers use a point as decimal mark and can be entered with exponential notation:

Examples:

2 # Result: 2
3.14 # Result: 3.14
1.4e3 # Result: 1400
22e-3 # Result: 0.022

Converting Numbers and Strings:

number("2.3") # Result: 2.3
string(2.3) # Result: "2.3"

Binary, Octal, and Hexadecimal:

0b11 # Result: 3 (binary)
0o77 # Result: 63 (octal)
0xff # Result: 255 (hexadecimal)
0xffi8 # Result: -1 (with word size)

Non-decimal with Radix Point:

0b1.1 # Result: 1.5
0o1.4 # Result: 1.5
0x1.8 # Result: 1.5

Formatting Numbers:

format(3, {notation: "bin"}) # Result: '0b11'
format(63, {notation: "oct"}) # Result: '0o77'
format(255, {notation: "hex"}) # Result: '0xff'
bin(-1, 8) # Result: '0b11111111i8'

Floating Point Precision:

0.1 + 0.2 # Result: 0.30000000000000004 (rounding error)
1e-325 # Result: 0 (underflow)
1e309 # Result: Infinity (overflow)

Examples:

true # Result: true
false # Result: false
(2 == 3) == false # Result: true

Converting Booleans:

number(true) # Result: 1
string(false) # Result: "false"
boolean(1) # Result: true
boolean("false") # Result: false

BigNumbers provide arbitrary precision for calculations:

Examples:

bignumber(0.1) + bignumber(0.2) # Result: 0.3 (exact)

Complex numbers can be created using the imaginary unit i:

Examples:

a = 2 + 3i # Result: 2 + 3i
b = 4 - i # Result: 4 - i
a + b # Result: 6 + 2i
a * b # Result: 11 + 10i
i * i # Result: -1
sqrt(-4) # Result: 2i
# Get real and imaginary parts
re(a) # Result: 2
im(a) # Result: 3

The calculator supports a comprehensive unit system for scientific and engineering calculations. Units can be combined, converted, and used in arithmetic operations.

Creating Units:

5.4 kg # Result: 5.4 kg
45 cm # Result: 45 cm
90 km/h # Result: 90 km / h
2 inch # Result: 2 inch
101325 kg/(m s^2) # Result: 101325 kg / (m s²) (Pascal)

Unit Conversions:

2 inch to cm # Result: 5.08 cm
20 celsius to fahrenheit # Result: ~68 fahrenheit
90 km/h to m/s # Result: 25 m / s
1 mile to km # Result: 1.609344 km
5 feet to meter # Result: 1.524 m

Arithmetic with Units:

0.5 kg + 33 g # Result: 0.533 kg
3 inch + 2 cm # Result: 3.7874 inch
12 seconds * 2 # Result: 24 seconds
80 mi/h * 2 tonne # Calculate kinetic energy
5 m * 3 m # Result: 15 m² (area)
10 N / 2 kg # Result: 5 m / s² (acceleration)

Trigonometry with Angles:

sin(45 deg) # Result: 0.7071067811865475
cos(pi rad) # Result: -1
tan(1 rad) # Result: 1.5574077246549023
45 deg to rad # Result: 0.7853981633974483 rad

Unit Conversion to Numbers:

number(5 cm, mm) # Result: 50
(5 cm).toNumber('mm') # Result: 50
(1 hour).toNumber('s') # Result: 3600
CategoryUnits
Lengthmeter (m), inch (in), foot (ft), yard (yd), mile (mi), link (li), rod (rd), chain (ch), angstrom, mil
Aream², sqin, sqft, sqyd, sqmi, sqrd, sqch, sqmil, acre, hectare
Volumem³, litre (l, L, lt, liter), cc, cuin, cuft, cuyd, teaspoon, tablespoon
Liquid Volumeminim, fluiddram (fldr), fluidounce (floz), gill (gi), cup (cp), pint (pt), quart (qt), gallon (gal), beerbarrel (bbl), oilbarrel (obl), hogshead, drop (gtt)
Anglesrad (radian), deg (degree), grad (gradian), cycle, arcsec (arcsecond), arcmin (arcminute)
Timesecond (s, secs, seconds), minute (min, mins, minutes), hour (h, hr, hrs, hours), day (days), week (weeks), month (months), year (years), decade (decades), century (centuries), millennium (millennia)
Frequencyhertz (Hz)
Massgram (g), tonne, ton, grain (gr), dram (dr), ounce (oz), poundmass (lbm, lb, lbs), hundredweight (cwt), stick, stone
Electric Currentampere (A)
Temperaturekelvin (K), celsius (degC), fahrenheit (degF), rankine (degR)
Amount of Substancemole (mol)
Luminous Intensitycandela (cd)
Forcenewton (N), dyne (dyn), poundforce (lbf), kip
Energyjoule (J), erg, Wh, BTU, electronvolt (eV)
Powerwatt (W), hp
PressurePa, psi, atm, torr, bar, mmHg, mmH2O, cmH2O
Electricity & Magnetismampere (A), coulomb (C), watt (W), volt (V), ohm, farad (F), weber (Wb), tesla (T), henry (H), siemens (S), electronvolt (eV)
Binarybits (b), bytes (B)

Note: All units support plural forms (e.g., 5 meters instead of 5 meter). Surface and volume units can be expressed as powers of length units (e.g., 100 in^2 instead of 100 sqin).

Decimal Prefixes (Large):

NameSymbolValue
decada10¹
hectoh10²
kilok10³
megaM10⁶
gigaG10⁹
teraT10¹²
petaP10¹⁵
exaE10¹⁸
zettaZ10²¹
yottaY10²⁴
ronnaR10²⁷
quettaQ10³⁰

Decimal Prefixes (Small):

NameSymbolValue
decid10⁻¹
centic10⁻²
millim10⁻³
microu10⁻⁶
nanon10⁻⁹
picop10⁻¹²
femtof10⁻¹⁵
attoa10⁻¹⁸
zeptoz10⁻²¹
yoctoy10⁻²⁴
rontor10⁻²⁷
quectoq10⁻³⁰

Binary Prefixes (for bits and bytes):

NameSymbolValue
kibiKi1024
mebiMi1024²
gibiGi1024³
tebiTi1024⁴
pebiPi1024⁵
exiEi1024⁶
zebiZi1024⁷
yobiYi1024⁸

Examples:

5 km # Result: 5000 m
100 GB # Result: 100 gigabytes
2.5 GHz # Result: 2500000000 Hz
1 KiB # Result: 1024 bytes (binary prefix)

Temperature Caution: Temperature scales like celsius and fahrenheit can behave unexpectedly in calculations because all operations work on the SI (Kelvin) representation. For reliable calculations, use kelvin (K) or rankine (degR):

14 degF * 2 # Result: 28 degF (270.93 K, not 526.3 K!)
abs(-13 degF) # Result: -13 degF (248.15 K), not 13 degF

Complex Unit Expressions: Use explicit operators and parentheses for clarity with complex units:

8.314 m^3 Pa / mol / K # Correct: 8.314 (m³ Pa) / (mol K)
8.314 (m^3 * Pa) / (mol * K) # Explicit form (recommended)
8.314 m^3 Pa / mol K # Wrong! Missing second '/' gives incorrect result

Strings are enclosed by double quotes " or single quotes ':

Examples:

"hello" # Result: "hello"
'hello' # Result: "hello"
a = concat("hello", " world") # Result: "hello world"
size(a) # Result: [11]
# String indexing and manipulation
a[1:5] # Result: "hello"
a[1] = "H" # Result: "H"
a[7:12] = "there!" # Result: "there!"
a # Result: "Hello there!"
# String conversion
number("300") # Result: 300
string(300) # Result: "300"

Matrices can be created using square brackets:

Examples:

[1, 2, 3] # Result: [1, 2, 3] (size [3])
[[1, 2, 3], [4, 5, 6]] # Result: [[1, 2, 3], [4, 5, 6]] (size [2, 3])
[1, 2, 3; 4, 5, 6] # Result: [[1, 2, 3], [4, 5, 6]] (size [2, 3])
[[[1, 2], [3, 4]], [[5, 6], [7, 8]]] # Result: 3D matrix (size [2, 2, 2])
# Initialize matrices
zeros(3, 2) # Result: [[0, 0], [0, 0], [0, 0]]
ones(3) # Result: [1, 1, 1]
5 * ones(2, 2) # Result: [[5, 5], [5, 5]]
identity(2) # Result: [[1, 0], [0, 1]]
1:4 # Result: [1, 2, 3, 4]
0:2:10 # Result: [0, 2, 4, 6, 8, 10]

Matrix indexing (one-based):

Examples:

a = [1, 2; 3, 4] # Result: [[1, 2], [3, 4]]
a[1, 1] # Result: 1
a[2, :] # Result: [3, 4]
a[1:2, 2] # Result: [2, 4]
# Using 'end' keyword
c = 5:9 # Result: [5, 6, 7, 8, 9]
c[end - 1 : -1 : 2] # Result: [8, 7, 6] (reverse with step)
# Modifying matrices
b = zeros(2, 2) # Result: [[0, 0], [0, 0]]
b[1, 1:2] = [5, 6] # Result: [[5, 6], [0, 0]]
b[2, :] = [7, 8] # Result: [[5, 6], [7, 8]]
# Matrix calculations
d = a * b # Result: [[19, 22], [43, 50]]
d[2, 1] # Result: 43
d[2, 1:end] # Result: [43, 50]

Objects are enclosed by curly brackets:

Examples:

{a: 2 + 1, b: 4} # Result: {a: 3, b: 4}
{"a": 2 + 1, "b": 4} # Result: {a: 3, b: 4}
{a: 2, b: {c: 3, d: 4}} # Result: {a: 2, b: {c: 3, d: 4}}
# Access properties
obj = {prop: 42}; obj.prop # Result: 42
obj = {prop: 42}; obj["prop"] # Result: 42
# Set properties (returns the whole object)
obj = {a: 12}
obj.prop = 43 # Result: {a: 12, prop: 43}
obj["prop"] = 43 # Result: {a: 12, prop: 43}

Implicit multiplication allows natural notation without the * operator:

Examples:

2 pi # Result: 6.283185307179586
(1+2)(3+4) # Result: 21

Important notes:

  • Implicit multiplication has higher precedence than explicit multiplication
  • Division is evaluated before implicit multiplication in patterns like 20 / 4 kg

Examples:

ExpressionInterpreted AsResult
(1 + 3) pi(1 + 3) * pi12.566370614359172
(4 - 1) 2(4 - 1) * 26
3 / 4 mm(3 / 4) * mm0.75 mm
2 + 3 i2 + (3 * i)2 + 3i
(1 + 2) (4 - 2)(1 + 2) * (4 - 2)6
sqrt(4) (1 + 2)sqrt(4) * (1 + 2)6
8 pi / 2 pi(8 * pi) / (2 * pi)4
pi / 2 pipi / (2 * pi)0.5
1 / 2i(1 / 2) * i0.5 i
8.314 J / mol K8.314 J / (mol * K)8.314 J / (mol * K)
20 kg / 4 kg(20 kg) / (4 kg)5
20 / 4 kg(20 / 4) kg5 kg

Expressions can span multiple lines using newline or semicolon ;:

Examples:

# Multiple statements (each on new line)
1 * 3
2 * 3
3 * 3
# Results: 3, 6, 9
# Semicolon hides output
a=3; b=4; a + b
a * b
# Results: 7, 12 (a=3 and b=4 are hidden)
# Expression spread over multiple lines
a = 2 +
3
# Result: 5

Comments start with # and end at the end of the line:

Examples:

# define some variables
width = 3 # Result: 3
height = 4 # Result: 4
width * height # calculate the area
# Result: 12
What is an expression calculator?

An expression calculator is a mathematical tool that evaluates complex mathematical expressions using proper mathematical notation. Unlike basic calculators that only handle simple operations, an expression calculator can process multi-step calculations, functions, variables, matrices, and units in a single expression like 2 * pi * 5^2 + sqrt(16).

How do I calculate powers and exponents?

Use the ^ operator for exponentiation. For example: 2^3 gives 8, 5^2 gives 25. You can also use the pow() function: pow(2, 3) also gives 8. For element-wise power on arrays, use .^ like [2,3] .^ [3,3] which gives [8, 27].

Can I use variables in my calculations?

Yes! Define variables using the = operator. For example: a = 5, then b = 10, then a * b gives 50. Variables persist throughout your session, so you can reference them in later calculations. Variable names must start with a letter, underscore, or dollar sign.

How do I calculate square roots?

Use the sqrt() function. For example: sqrt(25) gives 5, sqrt(144) gives 12. For negative numbers, it returns complex numbers: sqrt(-4) gives 2i. You can also use fractional exponents: 25^0.5 is equivalent to sqrt(25).

What mathematical constants are available?

Common constants include: pi or PI (3.14159…), e or E (2.71828…), tau (6.28318…), phi (golden ratio, 1.618…), i (imaginary unit), Infinity, SQRT2 (1.414…), LN2, LN10, and many more. Just type the constant name in your expression.

How do I use trigonometric functions?

Use functions like sin(), cos(), tan(), asin(), acos(), atan(). By default, they work in radians: sin(pi/2) gives 1. To use degrees, add the deg unit: sin(90 deg) also gives 1. Inverse functions return radians: asin(1) gives approximately 1.5708.

Can I convert between units?

Yes! Use the to or in keyword. Examples: 2 inch to cm gives 5.08 cm, 20 celsius to fahrenheit gives 68°F, 90 km/h to m/s gives 25 m/s. You can also combine units in calculations: 5 kg * 2 m/s^2 gives 10 N (force).

How do I create and use matrices?

Create matrices with square brackets: [1, 2, 3] for a row vector, [[1, 2], [3, 4]] for a 2x2 matrix. Use semicolons to separate rows: [1, 2; 3, 4]. Access elements with 1-based indexing: A[1, 2] gets the element at row 1, column 2. Matrix operations include * for multiplication, inv() for inverse, det() for determinant.

What is implicit multiplication?

Implicit multiplication allows you to omit the * operator for natural mathematical notation. For example: 2 pi equals 2 * pi, (1+2)(3+4) equals (1+2) * (3+4) giving 21. It has higher precedence than explicit multiplication, so 8 pi / 2 pi equals (8*pi) / (2*pi) which is 4.

How do I define custom functions?

Define functions by assigning an expression to a function call. Examples: f(x) = x^2 creates a squaring function, then f(5) gives 25. Multi-parameter functions: g(x, y) = x^y, then g(2, 3) gives 8. You can pass functions as parameters: twice(func, x) = func(func(x)).

Can I use parentheses to control calculation order?

Yes! Parentheses override the default operator precedence. For example: 2 + 3 * 4 gives 14 (multiplication first), but (2 + 3) * 4 gives 20 (addition first). Use parentheses liberally to make your expressions clear and ensure correct order of operations.

How do I calculate percentages?

Use the % operator: 8% gives 0.08. For percentage additions: 100 + 3% gives 103. For percentage subtractions: 100 - 3% gives 97. For modulus (remainder), use mod: 8 mod 3 gives 2, or the % operator in context: 8 % 3.

What are complex numbers and how do I use them?

Complex numbers use the imaginary unit i where i * i = -1. Create them like 2 + 3i or 4 - i. Operations work naturally: (2 + 3i) + (4 - i) gives 6 + 2i. Functions like sqrt(-4) return 2i. Extract parts with re() and im(): re(2 + 3i) gives 2, im(2 + 3i) gives 3.

How do I use logarithms?

Use log() for natural logarithm (base e): log(e) gives 1. For other bases, pass two arguments: log(100, 10) gives 2 (log base 10 of 100). Also available: log10() for base-10 and log2() for base-2. The inverse is exp(): exp(1) gives e.

Can I perform bitwise operations?

Yes! Use & (AND), | (OR), ^| (XOR), ~ (NOT), &lt;&lt; (left shift), &gt;&gt; (right shift), &gt;&gt;&gt; (unsigned right shift). Examples: 5 & 3 gives 1, 5 | 3 gives 7, 4 &lt;&lt; 1 gives 8. These work on integers and follow standard bitwise operation rules.

How do I use logical operators?

Logical operators include and, or, not, xor. Examples: true and false gives false, true or false gives true, not true gives false. They use lazy evaluation: in false and x, x is not evaluated. Comparison operators (&lt;, &gt;, ==, !=) can be chained: 5 &lt; x &lt; 10 means 5 &lt; x and x &lt; 10.

What statistical functions are available?

Statistical functions include: mean() for average, median() for middle value, std() for standard deviation, variance() for variance, min() and max() for extremes, sum() for total, prod() for product. Example: mean([1, 2, 3, 4, 5]) gives 3.

How do I round numbers?

Use round() to round to nearest integer: round(3.7) gives 4. Use floor() to round down: floor(3.7) gives 3. Use ceil() to round up: ceil(3.2) gives 4. Use fix() to round towards zero: fix(-3.7) gives -3. Add a second parameter for decimal places: round(3.14159, 2) gives 3.14.

Can I work with different number bases like binary or hexadecimal?

Yes! Prefix with 0b for binary: 0b11 gives 3. Use 0o for octal: 0o77 gives 63. Use 0x for hexadecimal: 0xff gives 255. Convert back with format(): format(255, &lbrace;notation: "hex"&rbrace;) gives “0xff”. These also support radix points: 0b1.1 gives 1.5.

How do I use the ternary conditional operator?

Use condition ? valueIfTrue : valueIfFalse. Examples: 15 &gt; 100 ? 1 : -1 gives -1, x &gt; 0 ? "positive" : "negative" returns different strings based on x. This is useful for conditional calculations without defining separate functions.

What are ranges and how do I create them?

Use the : operator to create ranges. 1:4 creates [1, 2, 3, 4]. Specify step with three values: 0:2:10 creates [0, 2, 4, 6, 8, 10]. Negative steps work too: 10:-2:0 creates [10, 8, 6, 4, 2, 0]. Ranges are useful for loops and array creation.

How do I access matrix elements?

Use 1-based indexing with square brackets. For matrix A = [[1, 2], [3, 4]]: A[1, 1] gives 1, A[2, 2] gives 4. Use : for entire rows/columns: A[1, :] gives [1, 2], A[:, 2] gives [2, 4]. Use ranges: A[1:2, 1] gives [1, 3]. The end keyword refers to the last index.

Can I use physical constants in my calculations?

Yes! The calculator includes constants like speedOfLight (299792458 m/s), gravitationConstant, planckConstant, avogadro (6.022e23), boltzmann, electronMass, protonMass, and many more. Example: speedOfLight * 5 s calculates distance light travels in 5 seconds.

How do I use the map function?

The map() function applies a function to each element of an array. Examples: map([1, 2, 3, 4], sqrt) gives [1, 1.414, 1.732, 2]. You can define inline functions: map([1, 2, 3, 4], f(x) = x^2) gives [1, 4, 9, 16]. Or use expressions: map([1, 2, 3, 4], x^2).

What’s the difference between BigNumber and regular numbers?

Regular numbers use JavaScript’s floating-point arithmetic, which can have rounding errors: 0.1 + 0.2 gives 0.30000000000000004. BigNumbers provide arbitrary precision: bignumber(0.1) + bignumber(0.2) gives exactly 0.3. Use BigNumbers when exact decimal arithmetic is critical, like financial calculations.

How do I multiply matrices?

Use * for matrix multiplication: [[1, 2], [3, 4]] * [[5, 6], [7, 8]] performs matrix multiplication. For element-wise multiplication, use .*: [1, 2, 3] .* [2, 3, 4] gives [2, 6, 12]. The dimensions must be compatible: for A * B, columns of A must equal rows of B.

Can I use scientific notation?

Yes! Use e for scientific notation: 1.4e3 equals 1400, 22e-3 equals 0.022, 6.022e23 represents Avogadro’s number. You can perform calculations directly: 1e6 * 2e3 gives 2e9 (2 billion). This is useful for very large or very small numbers.

How do I calculate factorials?

Use the ! operator after a number: 5! gives 120, 10! gives 3628800. Factorials only work on non-negative integers. For large factorials, consider using the gamma() function: gamma(n+1) equals n! but also works for non-integers.

What string operations are supported?

Create strings with quotes: "hello" or 'hello'. Concatenate with concat(): concat("hello", " world"). Get length with size(): size("hello") gives [5]. Index strings: "hello"[1] gives “h”. Extract substrings: "hello world"[1:5] gives “hello”. Convert with string() and number().

How do I create identity matrices?

Use identity(n) or eye(n) to create an n×n identity matrix. Example: identity(3) gives [[1, 0, 0], [0, 1, 0], [0, 0, 1]]. For non-square identity matrices, use eye(m, n): eye(2, 3) gives [[1, 0, 0], [0, 1, 0]].

Can I write multi-line expressions?

Yes! Separate statements with newlines or semicolons. Example: a = 5; b = 10; a * b executes all three statements. Semicolons hide intermediate results. You can also break long expressions across lines: result = 2 + 3 + 4 + 5 + 6 can be written as result = 2 + 3 + on one line and 4 + 5 + 6 on the next.

How do I add comments to my calculations?

Use # to start a comment that extends to the end of the line. Example: width = 5 # in meters, height = 3 # in meters, area = width * height # calculate area. Comments are ignored during calculation and help document your work.

What is the precedence of operators?

Operators are evaluated in order of precedence (highest to lowest): parentheses/brackets, function calls, transpose, factorial, exponentiation, unary operators, percentage, implicit multiplication, explicit multiplication/division, addition/subtraction, ranges, comparisons, logical operators, assignment. Use parentheses when in doubt: (2 + 3) * 4 vs 2 + 3 * 4.

How do I transpose a matrix?

Use the ' operator or transpose() function. Example: [[1, 2], [3, 4]]' gives [[1, 3], [2, 4]]. This swaps rows and columns. For complex matrices, ' is the conjugate transpose. Use transpose() for non-conjugate transpose of complex matrices.

Can I calculate matrix determinants?

Yes! Use det() function on square matrices. Example: det([[1, 2], [3, 4]]) gives -2. The determinant is only defined for square matrices. It’s useful for checking if a matrix is invertible (non-zero determinant) and in various linear algebra applications.

How do I invert a matrix?

Use the inv() function on square, non-singular matrices. Example: inv([[1, 2], [3, 4]]) gives [[-2, 1], [1.5, -0.5]]. Verify: A * inv(A) gives the identity matrix. If the determinant is zero, the matrix is not invertible.

What is the nullish coalescing operator?

The ?? operator returns the right operand when the left is null or undefined, otherwise returns the left operand. Example: null ?? 2 gives 2, 5 ?? 2 gives 5, undefined ?? "default" gives “default”. This is useful for providing default values.

How do I use optional chaining?

The ?. operator safely accesses nested object properties. If the left side is null or undefined, it returns undefined instead of throwing an error. Example: obj?.prop?.nested safely accesses deeply nested properties even if intermediate properties don’t exist.

Can I create 3D or higher-dimensional matrices?

Yes! Nest arrays to create multi-dimensional matrices. Example: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] creates a 2×2×2 3D matrix. Access elements with multiple indices: A[1, 1, 2]. Many operations work on n-dimensional arrays.

How do I calculate absolute values?

Use the abs() function. Example: abs(-5) gives 5, abs(3) gives 3. For complex numbers, it returns the magnitude: abs(3 + 4i) gives 5 (since sqrt(3² + 4²) = 5). This works with matrices too, applying element-wise.

What hyperbolic functions are available?

Hyperbolic functions include sinh(), cosh(), tanh(), asinh(), acosh(), atanh(). Example: sinh(0) gives 0, cosh(0) gives 1. These are useful in calculus, physics, and engineering applications involving hyperbolas and exponential relationships.

How do I create zero and one matrices?

Use zeros(m, n) for an m×n matrix of zeros: zeros(2, 3) gives [[0, 0, 0], [0, 0, 0]]. Use ones(m, n) for ones: ones(2, 2) gives [[1, 1], [1, 1]]. Single parameter creates square matrices or vectors: zeros(3) gives [0, 0, 0].

Can I calculate dot products and cross products?

Yes! Use dot() for dot product: dot([1, 2, 3], [4, 5, 6]) gives 32. Use cross() for cross product of 3D vectors: cross([1, 0, 0], [0, 1, 0]) gives [0, 0, 1]. These are essential operations in vector mathematics and physics.

How do I handle infinity and NaN?

Use Infinity for positive infinity, -Infinity for negative. NaN represents “Not a Number”. Examples: 1 / 0 gives Infinity, 0 / 0 gives NaN, log(-1) gives NaN. Check with isNaN() and isFinite(): isNaN(0/0) gives true, isFinite(1/0) gives false.

What’s the difference between = and ==?

= is the assignment operator: x = 5 assigns 5 to variable x. == is the equality comparison operator: 5 == 5 gives true, 5 == 3 gives false. Use != for inequality: 5 != 3 gives true. Always use == when comparing values, not =.

How do I calculate GCD and LCM?

Use gcd() for Greatest Common Divisor: gcd(12, 18) gives 6. Use lcm() for Least Common Multiple: lcm(12, 18) gives 36. These work with multiple arguments: gcd(12, 18, 24) gives 6. Useful for fraction simplification and number theory.

Can I use random numbers?

Yes! Use random() for a random number between 0 and 1: random() gives something like 0.7392. Use randomInt(max) or randomInt(min, max) for integers: randomInt(1, 6) simulates a die roll. Use pickRandom(array) to select a random element from an array.

How do I check if a number is prime?

While there’s no built-in isPrime() function in the basic calculator, you can create a custom function: isPrime(n) = n &gt; 1 and sum(map(1:n, x -&gt; n mod x == 0)) == 2. This counts divisors - a prime has exactly 2 divisors (1 and itself).