📘 Lesson  ·  Lesson 08

Numbers and Math

Numbers और Math

Numbers in JavaScript

let age = 20;           // integer (whole number)
let price = 99.50;      // decimal (floating point)
let temp = -5;          // negative
let big = 1000000;      // no commas allowed in code!
JavaScript has just ONE number type — unlike languages that separate integers from decimals. Whole numbers, decimals, and negatives are all simply number. This keeps things simple: you never worry about "is this an int or a float?" Write numbers plainly, with no quotes (quotes would make them strings) and no commas as thousands separators (1,000 is invalid — write 1000). A number in quotes, like "25", is a string, and behaves differently — a distinction that catches out many beginners.

Arithmetic — the basic operations

let a = 10, b = 3;

console.log(a + b);    // 13   addition
console.log(a - b);    // 7    subtraction
console.log(a * b);    // 30   multiplication
console.log(a / b);    // 3.333...  division
console.log(a % b);    // 1    modulus (the REMAINDER)
console.log(a ** 2);   // 100  exponent (a to the power 2)
Most operators are familiar, but two deserve attention. % is the modulus operator: it gives the REMAINDER after division (10 % 3 is 1, because 10 ÷ 3 leaves remainder 1). It's extremely useful for checking if a number is even (n % 2 === 0) or cycling through values. ** is the exponent operator (2 ** 3 is 8). Note that division in JavaScript always gives a decimal result if it doesn't divide evenly — 10 / 3 is 3.3333..., not 3. Modulus in particular appears constantly in real code, so it's worth remembering.

The Math Object — built-in helpers

Math.round(4.7);     // 5     nearest whole number
Math.round(4.3);     // 4
Math.floor(4.9);     // 4     always rounds DOWN
Math.ceil(4.1);      // 5     always rounds UP
Math.abs(-7);        // 7     absolute value (removes the minus)
Math.max(3, 9, 1);   // 9     largest
Math.min(3, 9, 1);   // 1     smallest
Math.sqrt(16);       // 4     square root
Math.pow(2, 3);      // 8     2 to the power 3
Math is a built-in object full of useful mathematical functions — you don't create it, it's just always available. The rounding trio is what you'll use most: Math.round() goes to the nearest whole number, Math.floor() always rounds DOWN (a "floor" is below you), and Math.ceil() always rounds UP (a "ceiling" is above you). That floor/ceiling image makes them easy to remember. Alongside these, Math.max() and Math.min() find the biggest/smallest of several numbers, and Math.abs() strips a negative sign. Handy tools you'll reach for often.

Math.random() — generating random numbers

Math.random();                          // a decimal between 0 and 1 (e.g. 0.734)

// A random whole number from 1 to 10:
Math.floor(Math.random() * 10) + 1;

// A random whole number from 1 to 6 (a dice roll):
Math.floor(Math.random() * 6) + 1;
Math.random() gives a random decimal between 0 (inclusive) and 1 (exclusive) — on its own, rarely what you want. The classic pattern to get a whole number in a range: multiply by the range size, apply Math.floor() to chop off the decimal, then add the starting number. So Math.floor(Math.random() * 6) + 1 gives 1–6, a dice roll. Memorise this pattern — random numbers power games, quizzes, random quotes, and picking random items. It's one of the most-used snippets in beginner JavaScript projects.

NaN, Infinity, and toFixed

console.log(0 / 0);            // NaN  — "Not a Number"
console.log("abc" * 2);        // NaN  — can't multiply text
console.log(10 / 0);           // Infinity

let price = 9.98765;
console.log(price.toFixed(2)); // "9.99" — 2 decimal places (returns a STRING)
NaN means "Not a Number" — the result you get when a maths operation makes no sense, like multiplying text. Confusingly, typeof NaN is "number"! If a calculation shows NaN, it usually means a string sneaked into your maths. Infinity appears when you divide by zero. Finally, toFixed(2) is invaluable for money: it rounds a number to a set number of decimal places — perfect for displaying prices as ₹9.99. Note it returns a STRING, not a number, so use it for display, not further maths.

Exam Corner

Q: How many number types does JavaScript have? One — number covers integers and decimals.

Q: What does the % operator do? Returns the remainder after division (modulus).

Q: Difference between Math.floor and Math.ceil? floor always rounds down; ceil always rounds up.

Q: How do you get a random whole number from 1 to 6? Math.floor(Math.random() * 6) + 1

Q: What is NaN and what does toFixed(2) return? NaN = "Not a Number" (invalid maths result); toFixed(2) returns a string rounded to 2 decimals.

JavaScript में Numbers

let age = 20;           // integer (poorn sankhya)
let price = 99.50;      // decimal (floating point)
let temp = -5;          // negative
let big = 1000000;      // code me commas allowed nahi!
JavaScript में सिर्फ EK number type है — उन languages के उलट जो integers को decimals से अलग करती हैं. पूर्ण संख्याएं, decimals, और negatives सब बस number हैं. यह चीज़ें सरल रखता है: आप कभी चिंता नहीं करते "यह int है या float?" Numbers सादे लिखिए, बिना quotes (quotes उन्हें strings बना देंगे) और बिना commas thousands separator के रूप में (1,000 invalid है — 1000 लिखिए). Quotes में number, जैसे "25", string है, और अलग व्यवहार करता है — यह भेद कई beginners को फंसाता है.

Arithmetic — बुनियादी operations

let a = 10, b = 3;

console.log(a + b);    // 13   addition
console.log(a - b);    // 7    subtraction
console.log(a * b);    // 30   multiplication
console.log(a / b);    // 3.333...  division
console.log(a % b);    // 1    modulus (SHESHFAL)
console.log(a ** 2);   // 100  exponent (a ki power 2)
ज़्यादातर operators परिचित हैं, पर दो ध्यान के लायक हैं. % modulus operator है: यह division के बाद SHESHFAL देता है (10 % 3 1 है, क्योंकि 10 ÷ 3 में शेष 1 बचता है). यह जांचने को बेहद उपयोगी कि number even है (n % 2 === 0) या values में चक्र लगाने को. ** exponent operator है (2 ** 3 8 है). ध्यान दीजिए JavaScript में division हमेशा decimal नतीजा देता है अगर पूरा भाग न हो — 10 / 3 3.3333... है, 3 नहीं. Modulus खासकर असली code में लगातार आता है, तो याद रखने लायक है.

Math Object — built-in helpers

Math.round(4.7);     // 5     nikattam poorn sankhya
Math.round(4.3);     // 4
Math.floor(4.9);     // 4     hamesha NEECHE round
Math.ceil(4.1);      // 5     hamesha UPAR round
Math.abs(-7);        // 7     absolute value (minus hatata)
Math.max(3, 9, 1);   // 9     sabse bada
Math.min(3, 9, 1);   // 1     sabse chhota
Math.sqrt(16);       // 4     square root
Math.pow(2, 3);      // 8     2 ki power 3
Math उपयोगी गणितीय functions से भरा built-in object है — आप इसे बनाते नहीं, यह बस हमेशा उपलब्ध है. Rounding तिकड़ी सबसे ज़्यादा use होगी: Math.round() निकटतम पूर्ण संख्या पर जाता है, Math.floor() हमेशा NEECHE round करता है ("floor" आपके नीचे होता है), और Math.ceil() हमेशा UPAR (छत आपके ऊपर होती है). वह floor/ceiling चित्र इन्हें याद रखना आसान बनाता है. इनके साथ, Math.max() और Math.min() कई numbers में सबसे बड़ा/छोटा ढूंढते हैं, और Math.abs() negative चिन्ह हटाता है. काम के tools जिन्हें आप अक्सर लेंगे.

Math.random() — random numbers बनाना

Math.random();                          // 0 aur 1 ke beech decimal (jaise 0.734)

// 1 se 10 tak random poorn sankhya:
Math.floor(Math.random() * 10) + 1;

// 1 se 6 tak random poorn sankhya (dice roll):
Math.floor(Math.random() * 6) + 1;
Math.random() 0 (शामिल) और 1 (बाहर) के बीच random decimal देता है — अकेले, शायद ही वह जो आप चाहते हैं. किसी range में पूर्ण संख्या पाने का classic pattern: range size से गुणा कीजिए, decimal काटने को Math.floor() लगाइए, फिर शुरुआती number जोड़िए. तो Math.floor(Math.random() * 6) + 1 1–6 देता है, dice roll. यह pattern याद कीजिए — random numbers games, quizzes, random quotes, और random items चुनने को शक्ति देते हैं. यह beginner JavaScript projects में सबसे ज़्यादा use होने वाले snippets में से एक है.

NaN, Infinity, और toFixed

console.log(0 / 0);            // NaN  — "Not a Number"
console.log("abc" * 2);        // NaN  — text ko multiply nahi kar sakte
console.log(10 / 0);           // Infinity

let price = 9.98765;
console.log(price.toFixed(2)); // "9.99" — 2 decimal places (STRING lautata hai)
NaN मतलब "Not a Number" — वह नतीजा जो आपको तब मिलता है जब कोई गणितीय operation बेमतलब हो, जैसे text को गुणा करना. उलझाने वाली बात, typeof NaN "number" है! अगर calculation NaN दिखाए, आमतौर पर मतलब आपके गणित में कोई string घुस गई. Infinity तब दिखता है जब आप शून्य से भाग दें. आखिर में, toFixed(2) पैसे के लिए अमूल्य है: यह number को तय decimal places तक round करता है — prices को ₹9.99 दिखाने के लिए perfect. ध्यान यह STRING लौटाता है, number नहीं, तो इसे display के लिए use कीजिए, आगे गणित के लिए नहीं.

Exam Corner

Q: JavaScript में कितने number types हैं? एक — number integers और decimals दोनों cover करता है.

Q: % operator क्या करता है? Division के बाद शेषफल लौटाता है (modulus).

Q: Math.floor और Math.ceil में अंतर? floor हमेशा नीचे round करता; ceil हमेशा ऊपर.

Q: 1 से 6 तक random पूर्ण संख्या कैसे पाते हैं? Math.floor(Math.random() * 6) + 1

Q: NaN क्या है और toFixed(2) क्या लौटाता है? NaN = "Not a Number" (अमान्य गणित नतीजा); toFixed(2) 2 decimals तक rounded string लौटाता है.
← Back to JavaScript Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
👁 Live Preview
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का console.log देखने के लिए F12 दबाइए.