JavaScript operators are symbols or keywords used to perform operations on variables and values. They allow you to manipulate data and perform computations in your JavaScript code. JavaScript operators can be categorized into several types, including arithmetic, comparison, logical, assignment, and more. Here, I'll provide an overview of the most common types of JavaScript operators:
-
Arithmetic Operators:
+(Addition)-(Subtraction)*(Multiplication)/(Division)%(Modulus, returns the remainder)++(Increment by 1)--(Decrement by 1)
let x = 10; let y = 5; let sum = x + y; let remainder = x % y; x++; // Increment 'x' by 1 y--; // Decrement 'y' by 1
-
Comparison Operators:
==(Equal to)!=(Not equal to)===(Strict equal to)!==(Strict not equal to)>(Greater than)<(Less than)>=(Greater than or equal to)<=(Less than or equal to)
let a = 10; let b = 5; let isEqual = a === b; let isGreaterThan = a > b;
-
Logical Operators:
&&(Logical AND)||(Logical OR)!(Logical NOT)
let isTrue = true; let isFalse = false; let result = isTrue && isFalse; // Logical AND result = isTrue || isFalse; // Logical OR result = !isTrue; // Logical NOT
-
Assignment Operators:
=(Assignment)+=(Add and assign)-=(Subtract and assign)*=(Multiply and assign)/=(Divide and assign)%=(Modulus and assign)
let x = 10; x += 5; // Equivalent to x = x + 5 x -= 3; // Equivalent to x = x - 3
-
Ternary Operator (Conditional Operator):
condition ? expression_if_true : expression_if_false
let age = 20; let canVote = age >= 18 ? "Yes" : "No";
-
String Concatenation Operator:
+(Used to concatenate strings)
let firstName = "John"; let lastName = "Doe"; let fullName = firstName + " " + lastName;
-
Typeof Operator:
typeof(Used to check the data type of a value)
let x = 10; let type = typeof x; // Returns "number"
-
Comma Operator:
,(Used to separate expressions, and it returns the result of the last expression)
let a = 1, b = 2, c = 3; let result = (a++, b++, c++); // result will be 3
These are some of the most commonly used JavaScript operators. Understanding how to use these operators is crucial for performing various operations in JavaScript, from basic arithmetic calculations to more complex logic and decision-making in your code.
Create a program that converts degree celsius to fahrenheit. The formula to convert celsius to fahrenheit is Formula: fahrenheit = celsius * 1.8 + 32. What you can do is store the value of celsius value in the celsius variable. Then use the formula fahrenheit = celsius * 1.8 + 32 to convert celsius to fahrenheit and display the result.
let celcius = 88;
let fahrenheit = celcius * 1.8 + 32;
console.log(`${celcius} celcius equals to ${fahrenheit} fahrenheit`);Output
88 celcius equals to 190.4 fahrenheit
Q. What is the output of the following code?
console.log(5**3);- 555
- 15
- 125
- 8
Answer: 3