04 - JavaScript - Operators

04 - JavaScript - Operators

JavaScript operators are symbols or keywords used to perform various operations on one or more values, producing new values. There are several types of operators in JavaScript. Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, division, and modulus. Comparison operators test if two values are equal or not, while logical operators are used to test if a condition is true or false. Assignment operators store values in variables. Other operators include bitwise, string, and conditional operators, which enable more complex operations. JavaScript operators are useful for performing a range of operations on values, from simple arithmetic to complex logical operations. Whether you need to manipulate binary data, strings or create conditional statements, there is an operator for the job. Overall, JavaScript operators are crucial for web development and programming, making it an essential tool for developers.

Types of JS Operators

Types of JS Operators The different types of JS operators are as follows -

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • String Operators

  • Logical Operators

  • Bitwise Operators

  • Ternary Operators

  • Type Operators

Comparison Operators

JavaScript comparison operators are used to compare two values and return a Boolean value of true or false based on the comparison result. Here are some examples of comparison operators in JavaScript -

  1. Equal to (==): The equal to operator compares two values and returns true if they are equal. For example, 5 == 5 is true, while 5 == 6 is false.

  2. Not equal to (!=): The not equal to operator compares two values and returns true if they are not equal. For example, 5 != 6 is true, while 5 != 5 is false.

  3. Greater than (>): The greater than operator compares two values and returns true if the first value is greater than the second value. For example, 5 > 3 is true, while 3 > 5 is false.

  4. Less than (<): The less than operator compares two values and returns true if the first value is less than the second value. For example, 3 < 5 is true, while 5 < 3 is false.

  5. Greater than or equal to (>=): The greater than or equal to operator compares two values and returns true if the first value is greater than or equal to the second value. For example, 5 >= 5 is true, while 3 >= 5 is false.

  6. Less than or equal to (<=): The less than or equal to operator compares two values and returns true if the first value is less than or equal to the second value. For example, 3 <= 5 is true, while 5 <= 3 is false.

  7. Equal value and equal type (===): The equal value and equal type operator compare two values and returns true if they are equal in both value and type. For example, 5 === 5 is true, while 5 === '5' and '5' === 5 are both false.

  8. Not equal value or not equal type (!==): The not equal value or not equal type operator compares two values and returns true if they are not equal in value or not equal in type. For example, 5 !== '5' is true, while 5 !== 5 and '5' !== '5' are both false.

Overall, JavaScript comparison operators are used to evaluate conditions in programming and return true or false values based on the comparison result.

String Operators

In JavaScript, string operators are used to perform operations on strings. Here are some examples of string operators in JavaScript -

  1. Concatenation (+): The concatenation operator is used to join two or more strings together. For example, "Hello " + "world" will result in "Hello world".

  2. Concatenation assignment (+=): The concatenation assignment operator is used to add a new string to an existing string. For example, For example, "Hello " += "world" will result in "Hello world".

  3. String length (.length): The length property returns the length of a string. For example, "Hello world".length will return 11.

  4. String indexing ([]): The indexing operator is used to access a character or substring at a specific index in a string. For example, "Hello world"[0] will return "H", while "Hello world"[6] will return "w".

  5. String slicing (.slice()): The slice() method is used to extract a section of a string and returns a new string. For example, "Hello world".slice(0, 5) will return "Hello".

  6. String searching (.indexOf()): The indexOf() method is used to search for a substring within a string and returns the index of the first occurrence. For example, "Hello world".indexOf("world") will return 6.

  7. String replacing (.replace()): The replace() method is used to replace a substring within a string with a new substring and returns a new string. For example, "Hello world".replace("world", "John") will return "Hello John".

  8. String uppercase (.toUpperCase()): The toUpperCase() method is used to convert all characters in a string to uppercase and returns a new string. For example, "Hello world".toUpperCase() will return "HELLO WORLD".

  9. String lowercase (.toLowerCase()): The toLowerCase() method is used to convert all characters in a string to lowercase and returns a new string. For example, "Hello world".toLowerCase() will return "hello world".

Overall, string operators are used to manipulate strings in JavaScript and can be useful in various programming applications.

Summarizing Up

JavaScript comparison operators are used to compare values and return a true or false value, based on the comparison result. These operators include <, >, <=, >=, ==, !=, ===, !==. On the other hand, string operators in JavaScript are used to manipulate strings, including concatenation, length, indexing, slicing, searching, replacing, and converting to uppercase or lowercase. These operators are used to perform operations on strings in JavaScript and can be useful in various programming applications.

Did you find this article valuable?

Support manas krishna jaiswal by becoming a sponsor. Any amount is appreciated!