Operators

Basics

Operators are special characters or symbols used to perform operations on values or variables. These operators can be used for arithmetic, logical, and comparison operations. let’s discuss the operators supported by MagiScript and their usage.

Please note that some operators are not compatible with strings, and can cause the system to restart. +, === and !== are working with strings.

Arithmetic Operators

The arithmetic operators are used to perform basic mathematical operations on values or variables.

Addition (+)

The addition operator is represented by the + symbol and is used to add two or more values or variables together.

let a = 10;
let b = 20;
let c = a + b;
console.log(c); // Output: 30

Increment (++)

The increment operator is represented by the ++ symbol and is used to increase the value of a variable by 1.

let a = 10;
a++;
console.log(a); // Output: 11

Subtraction (-)

The subtraction operator is represented by the - symbol and is used to subtract one value from another.

let a = 10;
let b = 20;
let c = b - a;
console.log(c); // Output: 10

Decrement (—)

The decrement operator is represented by the -- symbol and is used to decrease the value of a variable by 1.

let a = 10;
a--;
console.log(a); // Output: 9

Division (/)

The division operator is represented by the / symbol and is used to divide one value by another.

let a = 20;
let b = 5;
let c = a / b;
console.log(c); // Output: 4

Modulo (%)

The modulo operator is represented by the % symbol and is used to find the remainder of a division operation.

let a = 20;
let b = 3;
let c = a % b;
console.log(c); // Output: 2

Multiplication (*)

The multiplication operator is represented by the * symbol and is used to multiply two or more values or variables together.

let a = 5;
let b = 2;
let c = a * b;
console.log(c); // Output: 10

Exponentiation ()

The exponentiation operator is represented by the `` symbol and is used to raise a value to a power.

_CODE_BLOCK7

Comparison Operators

The comparison operators are used to compare two values or variables and return a Boolean value (true or false).

Strict Equality (===)

The strict equality operator is represented by the === symbol and is used to check if two values are equal in both type and value.

_CODE_BLOCK8

Strict Inequality (!==)

The strict inequality operator is represented by the !== symbol and is used to check if two values are not equal in both type and value.

_CODE_BLOCK9

Greater Than (>)

The greater than operator is represented by the > symbol and is used to check if one value is greater than another.

_CODE_BLOCK10

Less Than (<)

The less than operator is represented by the < symbol and is used to check if one value is less than another.

_CODE_BLOCK11

Greater Than or Equal To (>=)

The greater than or equal to operator is represented by the >= symbol and is used to check if one value is greater than or equal to another.

_CODE_BLOCK12

Less Than or Equal To (<=)

The less than or equal to operator is represented by the <= symbol and is used to check if one value is less than or equal to another.

_CODE_BLOCK13

Bitwise Operators

The bitwise operators are used to perform operations on binary values.

Bitwise AND (&)

The bitwise AND operator is represented by the & symbol and is used to perform a logical AND operation on the bits of two values.

_CODE_BLOCK14

Bitwise OR (|)

The bitwise OR operator is represented by the | symbol and is used to perform a logical OR operation on the bits of two values.

_CODE_BLOCK15

Bitwise XOR (^)

The bitwise XOR operator is represented by the ^ symbol and is used to perform a logical XOR operation on the bits of two values.

_CODE_BLOCK16

Bitwise NOT (~)

The bitwise NOT operator is represented by the ~ symbol and is used to invert the bits of a value.

_CODE_BLOCK17

Bitwise Right Shift (>>)

The bitwise right shift operator is represented by the >> symbol and is used to shift the bits of a value to the right.

_CODE_BLOCK18

Bitwise Unsigned Right Shift (>>>)

The bitwise unsigned right shift operator is represented by the >>> symbol and is used to shift the bits of a value to the right, but it always fills the leftmost bits with 0.

_CODE_BLOCK19

Bitwise Left Shift (<<)

The bitwise left shift operator is represented by the << symbol and is used to shift the bits of a value to the left.

_CODE_BLOCK20

Other Operators

Further operations are listed below:

Logical NOT (!)

The logical NOT operator is represented by the ! symbol and is used to invert a Boolean value. If the value is true, it returns false. If the value is false, it returns true.

_CODE_BLOCK21

Conditional (Ternary) Operator (?:)

The conditional operator is represented by the ?: symbols and is used to create a short if-else statement. If the condition is true, it returns the first expression. If the condition is false, it returns the second expression.

let a = 5;
let b = 2;
let c = a > b ? "a is greater than b" : "a is less than or equal to b";
console.log(c); // Output: "a is greater than b"

Type of Operator

The type of operator is used to determine the type of a value or variable.

let a = 5;
let b = "Hello, world!";
let c = true;

console.log(typeof a); // Output: "number"
console.log(typeof b); // Output: "string"
console.log(typeof c); // Output: "boolean"

Conclusion

Operators are an essential part of any programming language, and MagiScript supports a wide range of operators for performing various operations. Understanding the use of the most common operators is crucial for writing efficient and effective MagiScript code.