| This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet as we cannot guarantee the safety of other websites. |
This JavaScript tutorial discusses operators, such as +, -, /, *, etc.
Here are lists of common operators in JavaScript, what they do, and what Operators Blocks do the same thing in Scratch.
Math Operators
These operators have numbers as their inputs and outputs.
The examples here for these operators have as their inputs variables named x and y.
x + y- Adds x and y.
((x) + (y))
x - y- Subtracts y from x.
((x) - (y))
x * y- Multiplies x and y.
((x) * (y))
x / y- Divides x and y.
((x) / (y))
x % y- Tells the remainder after dividing x / y.
((x) mod (y)), but copying the sign of the first argument instead of the second- To replicate
((x) mod (y)), dox - y * Math.floor(x / y)
x ** y- This is x to the power of y.
- There is no built-in block in Scratch to perform this operation, but some ways to do so with longer scripts are shown at Solving Exponents.
Math.round(x)- Rounds x to the closest whole number
round (x)
Math.random()- Picks a random decimal number between 0 and 1
pick random [0.0] to [1.0]
Many operations that use ([ v] of ()::operators) in Scratch use Math in JavaScript:
| Javascript | Scratch |
|---|---|
Math.abs(x)
|
([abs v] of (x))
|
Math.floor(x)
|
([floor v] of (x))
|
Math.ceil(x)
|
([ceiling v] of (x))
|
Math.sqrt(x)
|
([sqrt v] of (x))
|
Math.sin(x * Math.PI / 180), Math.cos(x * Math.PI / 180), Math.tan(x * Math.PI / 180)
|
([sin v] of (x)), ([cos v] of (x)), ([tan v] of (x))
|
Math.asin(x) * 180 / Math.PI, Math.acos(x) * 180 / Math.PI, Math.atan(x) * 180 / Math.PI
|
([asin v] of (x)), ([acos v] of (x)), ([atan v] of (x))
|
Math.log(x)
|
([ln v] of (x))
|
Math.log10(x)
|
([log v] of (x))
|
Math.log2(x)
|
(([ln v] of (x)) / ([ln v] of (2)))
|
Math.exp(x)
|
([e ^ v] of (x))
|
Math.pow(10, x)
|
([10 ^ v] of (x))
|
Logarithms With Other Bases
In JavaScript, the following function (it's almost like a custom block in Scratch) can be used to get the logarithmic value of a number in a specified base.[1]
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}To do this in Scratch, you can do:
([log v] of (y)) / ([log v] of (x))
By using this code, this enables log base 2, log base 64, and more.
Comparison Operators
These operators have numbers, and sometimes strings, as their inputs, and Boolean (true or false) values as their outputs.
The examples here for these operators have as their inputs variables named x and y.
x < y- This expression equals
trueif x is less than y. This expression equalsfalseif x is greater than y, if x is the same as y, or if x and y cannot be ordered. <(x) < (y)>
- This expression equals
x > y- This expression equals
trueif x is greater than y. This expression equalsfalseif x is less than y, if x is the same as y, or if x and y cannot be ordered. <(x) > (y)>
- This expression equals
x == y- This expression equals
trueif x and y represent the same number or string. This expression equalsfalseif x represents a different number and a different string than y, or if x and y are stored as separate Objects. <(x) = (y)>Note:
This is not the same as x === y, which also tests for equality, but in a different sense than Scratch's equality test.Caution:
This is not the same as x = y, which does not test for equality and will mess up variables if mistaken for ==.
- This expression equals
x != y- This expression equals
trueif x == y would equal false. This expression equalsfalseif x == y would equal true. <not <(x) = (y)>>Note:
This is not the same as x !== y, which also represents 'not equal', but in different sense.Note:
JavaScript, unlike Scratch, considers a letter different in different cases, so "A" != "a"is true.
- This expression equals
Logical Operators
These operators have Boolean values as their inputs and outputs.
The examples here for these operators have as their inputs comparisons between a variable named x and other variables.
(x > y) && (x > z)- Is x > y and x > z? If both of these are true, (x > y) && (x > z) equals
true. If at least one of them is false, (x > y) && (x > z) equalsfalse. <<(x) > (y)> and <(x) > (z)>>
- Is x > y and x > z? If both of these are true, (x > y) && (x > z) equals
(x > y) || (x > z)- Is x > y or x > z? If at least one of these is true, (x > y) || (x > z) equals
true. If both of them are false, (x > y) || (x > z) equalsfalse. <<(x) > (y)> or <(x) > (z)>>
- Is x > y or x > z? If at least one of these is true, (x > y) || (x > z) equals
! (x > y)- Is x > y? If so, then ! (x > y) equals
false. If not, then ! (x > y) equalstrue. <not <(x) > (y)>>
- Is x > y? If so, then ! (x > y) equals