← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 10 of 12

JS Operators

Think of a toolbox with different tools for different jobs, a hammer for nails, a wrench for bolts, a tape measure for checking size, each tool does one specific kind of work. JavaScript operators are those tools for working with values, arithmetic operators do math, comparison operators check how values relate to each other, and logical operators combine true and false results together. This chapter is a broad tour of the main operator families, each gets its own deeper chapter later, but seeing them together first helps when reading real code on sites like cookiescursor.com.
Syntax
javascript
result = operand1 operator operand2;

Arithmetic Operators

Arithmetic operators संख्याओं पर गणितीय गणनाएँ करते हैं, जिनमें जोड़, घटाव, गुणा, भाग, और शेषफल तथा exponentiation जैसे ज़्यादा विशिष्ट operations शामिल हैं।

Note: + operator strings को जोड़ने के लिए भी काम करता है, उसका व्यवहार पूरी तरह शामिल values के types पर निर्भर करता है।
Warning: Dividing by zero in JavaScript does not throw an error, it returns Infinity or -Infinity instead, which can hide bugs if not checked for.

उदाहरण: Arithmetic Operators

javascript
// Print `10 + 3` to the console
// Print `10 + 3` to the console
console.log(10 + 3);
// Print `10 % 3` to the console
// Print `10 % 3` to the console
console.log(10 % 3);
// Print `2 ** 3` to the console
// Print `2 ** 3` to the console
console.log(2 ** 3);

Comparison Operators

Comparison operators दो values की तुलना करते हैं और boolean नतीजा, true या false, देते हैं, जिनमें बराबर, बराबर नहीं, बड़ा और छोटा शामिल हैं, साथ ही सख्त संस्करण जो type भी जाँचते हैं।

Note: लगभग सभी मामलों में == और != की जगह सख्त === और !== को तरजीह दीजिए, वे type conversion से होने वाले चौंकाने वाले bugs से बचाते हैं।
Warning: == considers 5 (a string) and 5 (a number) equal after converting types, while === correctly treats them as different.

उदाहरण: Comparison Operators

javascript
// Print `5 == "5"` to the console
// Print `5 == "5"` to the console
console.log(5 == "5");
// Print `5 === "5"` to the console
// Print `5 === "5"` to the console
console.log(5 === "5");
// Print `5 > 3` to the console
// Print `5 > 3` to the console
console.log(5 > 3);

Logical Operators

Logical operators boolean values को जोड़ते या उलटते हैं, && (और) के लिए दोनों पक्षों का true होना ज़रूरी है, || (या) के लिए कम से कम एक पक्ष का true होना, और ! (नहीं) किसी boolean के value को पूरी तरह पलट देता है।

Note: Logical operators किसी if statement में कई शर्तों को अलग-अलग nest किए बिना जोड़ने के लिए ज़रूरी हैं।
Warning: && and || don't always return strict true or false, they return one of the original operand values, which matters in more advanced usage.

उदाहरण: Logical Operators

javascript
// Print `true && false` to the console
// Print `true && false` to the console
console.log(true && false);
// Print `true || false` to the console
// Print `true || false` to the console
console.log(true || false);
// Print `!true` to the console
// Print `!true` to the console
console.log(!true);

Assignment और Ternary Operators

Assignment operator = किसी variable में value रखता है, और JavaScript एक संक्षिप्त ternary operator भी देती है, condition ? valueIfTrue : valueIfFalse, जो value बनाने वाले सरल if/else के शॉर्टहैंड के रूप में है।

Note: जब आपको दो सरल values में से चुनना हो तब ternary operator अपनाइए, लेकिन ज़्यादा जटिल logic के लिए पूर्ण if/else का ही उपयोग करते रहिए।
Warning: Nesting multiple ternary operators together can quickly become difficult to read, prefer if/else once logic gets more complex.

उदाहरण: Assignment and Ternary Operators

javascript
// Declare the variable `age`, set to `20`
// Declare the variable `age`, set to `20`
let age = 20;
// Declare the variable `status`, set to `age >= 18 ? "adult" : "minor"`
// Declare the variable `status`, set to `age >= 18 ? "adult" : "minor"`
let status = age >= 18 ? "adult" : "minor";
// Print `status` to the console
// Print `status` to the console
console.log(status);

typeof और Spread Operators

typeof किसी value का type बताने वाली string लौटाता है, जो यह जाँचने में उपयोगी है कि आप किस तरह के data के साथ काम कर रहे हैं, जबकि spread operator ... किसी array या object के elements को अलग-अलग फैला देता है, जो आमतौर पर collections की नक़ल बनाने या उन्हें जोड़ने के लिए इस्तेमाल होता है।

Note: आगे प्रोसेस करने से पहले यह त्वरित जाँचने के लिए कि कोई value number, string या कुछ अप्रत्याशित है, typeof खास तौर पर काम का है।
Warning: typeof null famously returns object, a long-standing quirk in JavaScript that catches many developers off guard.

उदाहरण: typeof and Spread Operators

javascript
// Print `typeof "hello"` to the console
// Print `typeof "hello"` to the console
console.log(typeof "hello");
// Print `typeof null` to the console
// Print `typeof null` to the console
console.log(typeof null);
// Declare the constant `nums`, set to `[1, 2, 3]`
// Declare the constant `nums`, set to `[1, 2, 3]`
const nums = [1, 2, 3];
// Print `[...nums, 4, 5]` to the console
// Print `[...nums, 4, 5]` to the console
console.log([...nums, 4, 5]);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.