Operators (Arithmetic, Assignment, Comparison, Logical and Conditional)

JavaScript ના operators કુટુંબોમાં આવે છે: arithmetic (+ - * / %) ગણે છે, assignment (= +=) સંઘરે છે, comparison (=== > <) સાચું/ખોટું પૂછે છે, logical (&& || !) એમને જોડે છે, અને conditional ternary (cond ? a : b) બે મૂલ્યોમાંથી એક પસંદ કરે છે, અને === એ સલામત સરખાપણું છે જે પ્રકાર પણ તપાસે છે.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

એ ફી જે '2001' થઈને નીકળી

FestConnect ને 200 ની ફી માં 1 રૂપિયાનો પ્રક્રિયાનો ખર્ચ ઉમેરવો છે. એક student total = fee + charge લખે છે જ્યાં fee એ form માંથી લખાણ "200" તરીકે આવ્યું હતું. પરિણામ "2001" છપાય છે, 201 નહીં.

Operators એ JavaScript નાં ક્રિયાપદો છે: એ ઉમેરે છે, સરખાવે છે, અને નક્કી કરે છે. પણ + શું કરે છે એ એની બંને બાજુના પ્રકારો પર આધારિત છે. આ પાઠ operators ને પાંચ કુટુંબમાં ગોઠવે છે જેથી તમે દરેકનું અનુમાન કરી શકો.

Formula

પાંચ કુટુંબ

  • Arithmetic: + - * / % મૂલ્ય ગણે છે. (% એ શેષ છે; + strings ને ચોંટાડે પણ છે.)
  • Assignment: = સંઘરે છે; += -= *= જગ્યા પર જ સુધારે છે.
  • Comparison: === !== > < >= <= સાચું/ખોટું નો પ્રશ્ન પૂછે છે.
  • Logical: && (અને), || (કે), ! (નહીં) સાચા/ખોટા જવાબોને જોડે છે.
  • Conditional (ternary): condition ? a : b શરત સાચી હોય તો a પસંદ કરે છે, નહીં તો b.

Comparison અને logical એ booleans આપે છે; arithmetic મૂલ્યો આપે છે; assignment એમને સંઘરે છે.

At a glance

દરેક કુટુંબનું એક ઉદાહરણ

કુટુંબઉદાહરણપરિણામ
Arithmetic17 % 52 (શેષ)
Assignmentseats -= 1seats 1 થી ઘટે છે
Comparisonyear >= 2true કે false
Logicalpaid && seats > 0બંને હોય તો જ true
Conditionalseats > 0 ? "Open" : "Full"બેમાંથી એક

Practical

FestConnect ની ફી અને પાત્રતા

const FEE = 200;
let charge = 1;
let total = FEE + charge;        // arithmetic -> 201

let year = 3;
let paid = true;
let eligible = (year >= 2) && paid;   // logical -> true

let seats = 0;
let status = seats > 0 ? "Open" : "Full";  // ternary -> "Full"

console.log(total, eligible, status);

// the trap: '5' is text, 5 is a number
console.log(5 == "5");   // true  (loose: converts first)
console.log(5 === "5");  // false (strict: type differs)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Output ધારો

દરેક શું છાપે છે? (a) "200" + 1 (b) 200 + 1 (c) 10 % 3 (d) (4 > 2) || false. Tap કરતાં પહેલાં ચારેય ગણો.

Show the answer

(a) string તરીકે "201": string સાથેનું + ચોંટાડે છે, એટલે એ '200' ના છેડે '1' જોડે છે. (b) number તરીકે 201: બંને numbers છે, એટલે એ સરવાળો કરે છે. (c) 1: modulus એ 10 ને 3 થી ભાગતાં વધતી શેષ છે. (d) true: 4 > 2 એ true છે, અને true || ગમે તે એ true છે. (a) અને (b) નો ભેદ બરાબર '2001' વાળો bug છે: લખાણ વત્તા આંકડો ચોંટે છે.

Quiz

તમે `if (userInput === 5)` તપાસો છો પણ userInput એ form માંથી "5" string તરીકે આવ્યું, એટલે ચકાસણી નિષ્ફળ જાય છે. એક સહાધ્યાયી કહે છે 'એને બદલે ફક્ત == વાપરો'. વધુ સારો ઉકેલ કયો છે અને શા માટે?

  1. Input ને number માં ફેરવો (Number(userInput)) અને === જ રાખો
  2. બધે == પર જાઓ, કારણ કે એ પ્રકારને અવગણે છે
  3. મૂલ્ય સોંપવા === ને બદલે = વાપરો
  4. JavaScript માં string ને number સાથે સરખાવી શકાય જ નહીં
Show the answer

Input ને number માં ફેરવો (Number(userInput)) અને === જ રાખો

ખરી સમસ્યા એ છે કે જ્યાં તમે number ની અપેક્ષા રાખી ત્યાં string છે; એને Number(userInput) થી ફેરવો અને સલામત, ધારી શકાય એવી સરખામણી માટે કડક === જ રાખો (A). == પર જવાથી (B) bug છુપાય છે અને બીજા bugs નોતરાય છે (== ના રૂપાંતરના નિયમો આશ્ચર્યજનક છે). = વાપરવાથી (C) userInput ને 5 સોંપાઈ જાત, સરખામણી નહીં, જે જાણીતી ભૂલ છે. Strings અને numbers ને ચોક્કસ સરખાવી શકાય છે (D ખોટું છે). Data સુધારો, operator નહીં.

Watch out

Operator ના ફાંદા

1. == સામે ===: == પહેલાં પ્રકાર ફેરવે છે (5 == "5" એ true છે), === ફેરવતું નથી (5 === "5" એ false છે). આશ્ચર્ય ટાળવા === પસંદ કરો.

2. = સામે ==: એકલું = સોંપે છે; if (x = 5) એ x ને 5 ગોઠવે છે અને લગભગ ક્યારેય તમારો એ અર્થ હોતો નથી. શરતોમાં === વાપરો.

3. String પર + ચોંટાડે છે: લખાણ અને આંકડા ને + થી ભેળવવાથી સરવાળાને બદલે ચોંટે છે. Form નાં મૂલ્યો પહેલાં Number(...) થી ફેરવો.

Theory

આ આગળના નિર્ણયોને ખવડાવે છે

Comparison અને logical operators એ સાચાં/ખોટાં મૂલ્યો આપે છે જેના પર તરત પછીના પાઠમાં if, while, અને for સુકાન ફેરવે છે. year >= 2 && paid એ બરાબર એવી કસોટી છે જેના પર if ફંટાઈને નક્કી કરશે કે FestConnect નો student નોંધણી કરી શકે કે નહીં. અહીં booleans પાકાં કરો અને control structures સહેલાં થઈ જશે.

Summary

Key takeaways

  • પાંચ કુટુંબ: arithmetic (+ - * / %), assignment (= +=), comparison (=== > <), logical (&& || !), conditional (a ? b : c).
  • Comparison અને logical operators true/false પાછું આપે છે; arithmetic મૂલ્યો; assignment સંઘરે છે.
  • == કરતાં === પસંદ કરો: === મૂલ્ય અને પ્રકાર તપાસે છે, == પહેલાં પ્રકાર ફેરવે છે (5 == "5" એ true છે).
  • = સોંપે છે, == / === સરખાવે છે: શરતમાં ક્યારેય = ન વાપરો.
  • String સંડોવાયેલી હોય ત્યારે + ચોંટાડે છે; ઉમેરતાં પહેલાં form નું લખાણ Number() થી ફેરવો.
  • Memory hook: === કડક છે, == બેદરકાર.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Overview of JavaScript

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Operators (Arithmetic, Assignment, Comparison, Logical and Conditional) · Web Designing-1 (option A) · Gri-Learn