top of page
Writer's picturecompnomics

Operator in Javascript




Here's a JavaScript program showcasing various operators:

// Arithmetic Operators:
let num1 = 10;
let num2 = 5;

console.log("Addition:", num1 + num2); // Output: 15
console.log("Subtraction:", num1 - num2); // Output: 5
console.log("Multiplication:", num1 * num2); // Output: 50
console.log("Division:", num1 / num2); // Output: 2
console.log("Modulo (remainder):", num1 % num2); // Output: 0

// Comparison Operators:
console.log("Equal:", num1 === num2); // Output: false (strict equality)
console.log("Loose equal:", num1 == num2); // Output: true (loose equality)
console.log("Greater than:", num1 > num2); // Output: true
console.log("Less than:", num1 < num2); // Output: false

// Logical Operators:
let isSunny = true;
let isWeekend = false;

console.log("And:", isSunny && isWeekend); // Output: false (both must be true)
console.log("Or:", isSunny || isWeekend); // Output: true (at least one must be true)
console.log("Not:", !isSunny); // Output: false (inverse of true)

// Assignment Operators:
let value = 20;

console.log("Assignment:", value); // Output: 20
console.log("Addition assignment:", value += 10); // Output: 30 (adds and assigns)
console.log("Subtraction assignment:", value -= 5); // Output: 25 (subtracts and assigns)
console.log("Multiplication assignment:", value *= 2); // Output: 50 (multiplies and assigns)

// Increment/Decrement Operators:
let count = 1;

console.log("Post-increment:", count++); // Output: 1 (prints then increments)
console.log("Count:", count); // Output: 2
console.log("Pre-decrement:", --count); // Output: 1 (decrements then prints)

// String Operators:
let message1 = "Hello ";
let message2 = "World!";

console.log("Concatenation:", message1 + message2); // Output: Hello World!

// Other Operators:
let number = "123";

console.log("Typeof (check data type):", typeof number); // Output: string

// This is just a basic overview, explore more operators and their functionalities!

Remember, operators perform specific operations on values or variables. Choose the appropriate operator based on your desired outcome and data types.

29 views0 comments

Recent Posts

See All

Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page