Javascript Fundamentals #Basic Operators
Are you just starting out in programming? Are you wanting to learn the basics of javascript operators? If so, then this blog post is for you! We’ll cover all the essential javascript operators, from addition and subtraction to multiplication and division. Read on to find out everything you need to know about getting started with javascript basic operators.
Introduction to JavaScript Operators
Welcome to the world of JavaScript operators! JavaScript's operators are special symbols used to perform operations on one or more operands (values). Operators are an essential part of programming, and they can be used in a variety of ways. In this article, we'll cover the fundamentals of JavaScript operators and discuss best practices for using them.
There are five main categories of JavaScript operators: arithmetic operators, assignment operators, comparison operators, logical operators, and conditional operators. Let's take a look at each one in turn.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition ( ), subtraction (-), multiplication (*), division (/), and modulus (%). These operators all take two operands and return a single value.
let x = 5;
let y = 3;
// addition
console.log('x + y = ', x + y); // 8
// subtraction
console.log('x - y = ', x - y); // 2
// multiplication
console.log('x * y = ', x * y); // 15
// division
console.log('x / y = ', x / y); // 1.6666666666666667
// remainder
console.log('x % y = ', x % y); // 2
// increment
console.log('++x = ', ++x); // x is now 6
console.log('x++ = ', x++); // prints 6 and then increased to 7
console.log('x = ', x); // 7
// decrement
console.log('--x = ', --x); // x is now 6
console.log('x-- = ', x--); // prints 6 and then decreased to 5
console.log('x = ', x); // 5
//exponentiation
console.log('x ** y =', x ** y);
Assignment Operators
Assignment operators are used to assign values to variables. The most common assignment operator is the equal sign (=), which assigns the value of its right-hand operand to its left-hand operand. Other assignment operators include the addition assignment operator ( =) and the multiplication assignment operator (*=).
// = Operator
let x = 10;
// *= Operator
let x = 10;
x *= 5;
Comparison Operators
Comparison operators are used to compare values. These operators return either true or false depending on whether the comparison is true. Common comparison operators include less than (), equal to (==), and not equal to (!=).
// equal operator
console.log(2 == 2); // true
console.log(2 == '2'); // true
// not equal operator
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true
// strict equal operator
console.log(2 === 2); // true
console.log(2 === '2'); // false
// strict not equal operator
console.log(2 !== '2'); // true
console.log(2 !== 2); // false
Logical Operators
Logical operators are used to combine multiple conditions into a single statement. The most common logical operator is the AND operator (&&), which returns true if both conditions are true. Other logical operators include OR (||) and NOT (!).
// logical AND
console.log(true && true); // true
console.log(true && false); // false
// logical OR
console.log(true || false); // true
// logical NOT
console.log(!true); // false
Conditional Operators
Conditional operators are used to evaluate a condition and return different values depending on the outcome. The most common conditional operator is the ternary operator (?:), which takes three operands and evaluates the first one. If the first operand is true, the second is returned;
If (condition) {
// some executable code
} else {
// some code
}
OR ternary operator
Condition ? First statement : Second statement
Best Practices for Using JavaScript Operators
Now that you know the different types of JavaScript operators, let’s look at some best practices for using them.
- Be sure to use the correct operator for the job. For example, if you want to increment a number, use the Increment Operator ( ) instead of adding one to the original number.
- Utilize parentheses when using multiple operators in an expression. This helps clarify the order of operations and is especially useful when working with complex expressions.
- When comparing two values, use the strict equality operators (=== & !==) instead of the non-strict equality operators (== & !=). The strict equality operators check both value and type, while the non-strict equality operators only check value.
- Don’t forget to assign a value to a variable using the Assignment Operator (=) before using it in an expression.
- Use descriptive variable names when assigning values to make your code easier to read and understand.
By following these best practices when writing your JavaScript code, you can ensure it is more organized, efficient and easier to debug if something goes wrong.