Operators

Operators are mathematical actions that modify or create values. Several operators can be chained together to create complex logic in a single statement. A simple operation example is "heading = heading + 180" to add 180° to the current heading angle, like the program used in the Loop Forever example.

Remember to follow your order or operations when using multiple operators. Use the mnemonic "PEMDAS" to remember the rules; it stands for "Parentheses, Exponents, Multiplication, Division, Addition, Subtraction."


Basic

+ adds two values

- subtracts one value from the other

* multiplies two values

/ divides one value by another

** multiplies a value by itself a given number of times

% returns the remainder after division of one value by another

getRandomInt(min, max) Random Integer: Returns a random integer between 2 values, inclusive

getRandomFloat(min, max) Random Float: Returns a random float between 2 values, inclusive


Special Sphero Operators

Color Channel

Individual color channels of the main LED lights, from 0 - 255:

colorRed getMainLed().r is the red channel, from 0 - 255.

colorGreen getMainLed().g is the green channel, from 0 - 255.

colorBlue getMainLed().b is the blue channel, from 0 - 255.


Math

The Math object gives you access to important constants like pi and operations like square root, sine and log. These operators require the prefix Math. to be called:

var x = Math.PI;            // Returns PI: ~ 3.14159
var y = Math.sqrt(16);      // Returns the square root of 16: 4

Math Properties

E returns the base of natural logarithms (~ 2.718), aka "Euler's number"

LN2 returns the natural logarithm of 2 (~ 0.693)

LN10 returns the natural logarithm of 10 (~ 2.302)

LOG2E returns the base-2 logarithm of E (~ 1.442)

LOG10E returns the base-10 logarithm of E (~ 0.434)

PI returns PI (~ 3.14159)

SQRT1_2 returns the square root of ½ (~ 0.707)

SQRT2 returns the square root of 2 (~ 1.414)

Math Methods

abs(x) returns the absolute value, removing the sign

cbrt(x) returns the cube root of a value

ceiling(x) rounds a value up to the nearest integer value

clz32(x) returns the number of leading zero bits in the 32-bit binary representation of a value

exp(x) returns the value of e (Euler's number) to the x power

expm1(x) returns the value of e (Euler's number) to the x power, minus 1

hypot(x, y, ..., n) returns the square root of the sum of squares of its arguments

floor(x) rounds a value down to the nearest integer value

fround(x) returns the nearest single precision float (SPF) representation of a value, where an SPF is a value that occupies 4 bytes (32 bits)

imul(a, b) returns the result of the C-like 32-bit multiplication of the two parameters.

log(x) returns the natural logarithm (base e) of x

log1p(x) function returns the natural logarithm (base e) of 1 + a value

log2(x) returns the base 2 logarithm of a value

log10(x) returns the base 10 logarithm of a number

min(x, y, ..., n) returns the number with the lowest value

max(x, y, ..., n) returns the number with the highest value

pow(x, y) returns the value of x to the power of y

random() returns a random number between 0 and 1

round(x) rounds a value to the nearest integer value (whole number)

sign(x) returns the sign of a number: +1 for positive, -1 for negative, and 0 for zero

sqrt(x) returns the square root of x

trunc() returns the integer part of a value by removing any fractional digits

Trigonometry Enter 0-360° to get the ratio of sides in a right angle triangle. Notice that we convert radians to degrees for ease of use, but you could write your own trig functions to use radians if desired. To remember each formula try the mnemonic "SOH CAH TOA":

Trig

sin(x) returns the sine of x radians

cos(x) returns the cosine of x radians

tan(x) returns the tangent of x radians

asin(x) returns an angle between -PI/2 and PI/2, whose sine is x

acos(x) returns an angle between 0 and PI radians, whose cosine is x

atan(x) returns an angle between -PI/2 and PI/2 radians, whose tangent is x

atan2(opp, adj) returns an angle between -PI/2 and PI/2 radians, given the angle's opposite and adjacent sides

sinh(x) returns the hyperbolic arcsine of x radians

cosh(x) returns the hyperbolic cosine of x radians

tanh(x) returns the hyperbolic tangent of x radians

Number

JavaScript supports whole numbers (integers), and fractional numbers (floats):

var x = 34;       // integer
var y = 3.14;     // float

Extra large and extra small numbers can be written with scientific (exponent) notation:

var x = 123E+5;    // = 12300000
var y = 123E-6;    // = 0.000123 

Number Properties

MAX_VALUE returns the largest number possible in JavaScript

MIN_VALUE returns the smallest number possible in JavaScript

NEGATIVE_INFINITY represents negative infinity (returned on overflow)

NaN represents a "Not-a-Number" value

POSITIVE_INFINITY represents infinity (returned on overflow)

constructor returns the function that created JavaScript's Number prototype (not really a math function)

prototype allows you to add properties and methods to an object (not really a math function)

Number Methods

isFinite() checks whether a value is a finite number

isInteger() checks whether a value is an integer

isNaN() checks whether a value is Number.NaN

isSafeInteger() checks whether a value is a safe integer

toExponential(x) converts a number into an exponential notation

toFixed(x) formats a number with x numbers of digits after the decimal point

toPrecision(x) formats a number to x length

toString() converts a number to a string

valueOf() returns the primitive value of a number

Date

The Date object is used to work with dates and times. Date objects are created with new Date(). There are 4 to instantiate a date:

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

Date Properties

constructor returns the function that created the Date object's prototype

prototype allows you to add properties and methods to an object

Date Methods

getDate() returns the day of the month (from 1-31)

getDay() returns the day of the week (from 0-6)

getFullYear() returns the year (4 digits for dates between year 1000 and 9999) of the specified date

getHours() returns the hour (from 0-23)

getMilliseconds() returns the milliseconds (from 0-999)

getMinutes() returns the minutes (from 0-59)

getMonth() returns the month (from 0-11)

getSeconds() returns the seconds (from 0-59)

getTime() returns the number of milliseconds since midnight Jan 1 1970, and a specified date

getTimezoneOffset() returns the time difference between UTC time and local time, in minutes

getUTCDate() returns the day of the month, according to universal time (from 1-31)

getUTCDay() returns the day of the week, according to universal time (from 0-6)

getUTCFullYear() returns the year, according to universal time

getUTCHours() returns the hour, according to universal time (from 0-23)

getUTCMilliseconds() returns the milliseconds, according to universal time (from 0-999)

getUTCMinutes() returns the minutes, according to universal time (from 0-59)

getUTCMonth() returns the month, according to universal time (from 0-11)

getUTCSeconds() returns the seconds, according to universal time (from 0-59)

now() returns the number of milliseconds since midnight Jan 1, 1970

parse() parses a date string and returns the number of milliseconds since January 1, 1970

setDate() sets the day of the month of a date object

setFullYear() sets the year (4 digits for dates between year 1000 and 9999) of the date object, can also be used to set the month and day of month.

setHours() sets the hour of a date object

setMilliseconds() sets the milliseconds of a date object

setMinutes() set the minutes of a date object

setMonth() sets the month of a date object

setSeconds() sets the seconds of a date object

setTime() sets a date to a specified number of milliseconds after/before January 1, 1970

setUTCDate() sets the day of the month of a date object, according to universal time

setUTCFullYear() sets the year of a date object, according to universal time

setUTCHours() sets the hour of a date object, according to universal time

setUTCMilliseconds() sets the milliseconds of a date object, according to universal time

setUTCMinutes() set the minutes of a date object, according to universal time

setUTCMonth() sets the month of a date object, according to universal time

setUTCSeconds() set the seconds of a date object, according to universal time

toDateString() converts the date portion of a Date object into a readable string

toUTCString() converts a date object to a string, according to UTC (GMT)

toISOString() returns the date as a string, using the ISO standard

toJSON() returns the date as a string, formatted as a JSON date

toLocaleDateString() returns the date portion of a Date object as a string, using locale conventions

toLocaleTimeString() returns the time portion of a Date object as a string, using locale conventions

toLocaleString() ronverts a Date object to a string, using locale conventions toString() ronverts a Date object to a string

toTimeString() ronverts the time portion of a Date object to a string toUTCString() ronverts a Date object to a string, according to universal time

UTC() returns the number of milliseconds in a date since midnight of January 1, 1970, according to UTC time

valueOf() returns the primitive value of a Date object

Array

The Array object is used to store multiple values in a single variable. Array indexes are zero-based: The first element is 0, the second is 1, etc.

var robots = ["Sphero", "Ollie", "BB-8"];

Array Properties

constructor returns the function that created the Array object's prototype

length sets or returns the number of elements in an array

prototype allows you to add properties and methods to an Array object

Array Methods

concat() joins two or more arrays, and returns a copy of the joined arrays

copyWithin() copies array elements within the array, to and from specified positions

every() checks if every element in an array pass a test

fill() fill the elements in an array with a static value

filter() creates a new array with every element in an array that pass a test

find() returns the value of the first element in an array that pass a test

findIndex() returns the index of the first element in an array that pass a test

forEach() calls a function for each array element

indexOf() search the array for an element and returns its position

isArray() checks whether an object is an array

join() joins all elements of an array into a string

lastIndexOf() search the array for an element, starting at the end, and returns its position

map() creates a new array with the result of calling a function for each array element

pop() removes the last element of an array, and returns that element

push() adds new elements to the end of an array, and returns the new length

reduce() reduce the values of an array to a single value (going left-to-right)

reduceRight() reduce the values of an array to a single value (going right-to-left)

reverse() reverses the order of the elements in an array

shift() removes the first element of an array, and returns that element

slice() relects a part of an array, and returns the new array

some() checks if any of the elements in an array pass a test

sort() sorts the elements of an array

splice() adds/removes elements from an array

toString() converts an array to a string, and returns the result

unshift() adds new elements to the beginning of an array, and returns the new length

valueOf() returns the primitive value of an array

Boolean

JavaScript booleans can have one of two values: true or false.

Boolean(2 > 1);       // returns true
Boolean(1 > 2);       // returns false

Boolean Properties

constructor returns the function that created JavaScript's Boolean prototype

prototype allows you to add properties and methods to the Boolean prototype

Boolean Methods

toString() converts a boolean value to a string, and returns the result

valueOf() returns the primitive value of a boolean