Programming with JavaScript

⌘K
  1. Home
  2. Docs
  3. Programming with JavaScri...
  4. Introduction to JavaScrip...
  5. JavaScript Operators

JavaScript Operators

Operators in JavaScript is simply a build in function.

Arithmetic Operators

Arithmetic operators in JavaScript are used to perform mathematical calculations on numeric values.

Arithmetic operators in JavaScript are used to perform mathematical calculations on numeric values.he arithmetic operators work with numeric values, so if you try to use them with non-numeric values, JavaScript may try to implicitly convert the values to numbers before performing the operation, potentially leading to unexpected results.

Assignment Operators

Assignment operators in JavaScript are used to assign values to variables. They allow to store data in variables and perform operations on the variable’s current value.

Comparison Operators (Relational Operators)

Comparison operators in JavaScript are used to compare values and return a Boolean result (true or false) based on the comparison’s outcome. They are commonly used in conditional statements and loops to make decisions based on the values’ relationships. Here are the main comparison operators in JavaScript:

Logical Operators

Logical operators in JavaScript are used to combine or modify Boolean values and return a Boolean result (true or false) based on the evaluation of the operands. They are widely used to make decisions, perform conditional checks, and control the flow of programs. JavaScript has three main logical operators:

Logical Operators can work with any Boolean expressions, not just variables. They can be used with the result of comparisons or logical operations

Ternary Operators

Ternary operators take three operands and returns one value based on the evaluation of a condition, The syntax is given as,

condition ? expression1 : expression2
  • Condition: A condition that is evaluated to true or false
  • expression1: The value returned if the condition is true
  • expression2: The value returned if the condition is false

Bitwise Operators

Bitwise operators operate on the individual bits of numeric values. Though these operators are not used in day-to-day programming, they can be used when working with binary data.

Unary Operators

Unary Operators work with a single operand, performing an operation on it or manipulating its value. The unary operators are listed as:

Example

Code

const prompt = require("prompt-sync")(); // required to get input from user
// Get the first number from the user
let firstNumber = prompt("Enter the first number:");

// Convert the input (which is a string) to a number
firstNumber = parseFloat(firstNumber);

// Get the second number from the user
let secondNumber = prompt("Enter the second number:");

// Convert the input (which is a string) to a number
secondNumber = parseFloat(secondNumber);

// Perform arithmetic operations
let sum = firstNumber + secondNumber;
let difference = firstNumber - secondNumber;
let product = firstNumber * secondNumber;
let quotient = firstNumber / secondNumber;

// Display the results
console.log("Sum: " + sum);
console.log("Difference: " + difference);
console.log("Product: " + product);
console.log("Quotient: " + quotient);

Output

Loading

Views: 11

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments