Logo
Unit 3 – JavaScript Operators

JavaScript Operators

Duration: 5 minutes

Hello again, enthusiastic web developers!

As we continue to build on our knowledge of variables and data types, we’ll examine another crucial subject: operators. JavaScript provides a wide variety of operators that enable us to operate on our data.

Understanding JavaScript Operators

Operators in JavaScript are symbols used to perform specific operations on one or more operands (variables and values) and return a result. There are different types of operators:

Arithmetic Operators: Used to perform mathematical operations.

‘+’: Addition‘-’: Subtraction‘*’: Multiplication
‘/’: Division‘%’: Modulus (remainder)‘++’: Increment
‘–’: Decrement
Comparison Operators: Used to compare two values.
‘==’: Equal to‘===’: Strictly equal to (value and type)
‘!=’: Not equal to‘!==’: Strictly not equal to (value and type)
‘>’: Greater than‘<’: Less than
‘>=’: Greater than or equal to‘<=’: Less than or equal to
Assignment Operators: Used to assign values to variables.
‘=’: Assign‘+=’: Assign after adding‘-=’: Assign after subtracting
‘*=’: Assign after multiplying‘/=’: Assign after dividing
Logical Operators: Used to determine the logic between variables or values.

| ‘&&’: Logical AND | ‘||’: Logical OR | ‘!’: Logical NOT | | --- | --- | --- | | ‘&&’: Logical AND | ‘||’: Logical OR | ‘!’: Logical NOT | Creating Expressions with Operators

Let’s construct some JavaScript expressions using the different operators:

// Arithmetic Operators
let sum = 10 + 5;
console.log('Sum:', sum);
let difference = 10 - 5;
console.log('Difference:', difference);
let product = 10 * 2;
console.log('Product:', product);
// Comparison Operators
let isEqual = (10 == "10"); // true because it checks only the value, not the type
console.log('Is Equal:', isEqual);
let isStrictlyEqual = (10 === "10"); // false because it checks both value and type
console.log('Is Strictly Equal:', isStrictlyEqual);
// Assignment Operators
let x = 10;
x += 5; // equivalent to x = x + 5
console.log('x after +=:', x);
// Logical Operators
let result = (x > 5) && (x < 20); // true because both conditions are true
console.log('Logical AND Result:', result);

Exercise

Your turn! Create different phrases by combining the operators we’ve already discussed. Experiment with blending several sorts. For instance, use the modulus operator to generate an arithmetic expression or the !== comparison operator to observe its operation. You’ll get more proficient with these operators as you put them through more practice.

Conclusion

Well done on advancing further into JavaScript’s foundations! Operators form the backbone of many operations in JavaScript, giving you greater control and flexibility when writing your code. As we continue, we’ll encounter more complex tasks, but with the knowledge you’ve garnered here, you’re well-equipped to tackle them. Keep up the fantastic work!

Next Tutorial: Control Structures: Conditional Statements

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!