Important JavaScript Interview Questions In Hindi
Password Strength Checker In jQuery In Hindi
JavaScript Spread syntax In Hindi : Spread Operator In JavaScript
JavaScript Web Worker Example | Web Workers In JavaScript In Hindi
Syntax Error In JavaScript In Hindi | JS SyntaxError
What is Hub? and How Hub works?
what is switch ? And How switch works ?
Linux traceroute Command : traceroute Command In Linux
AWS S3-Snowflake Pipeline Example | Snowflake Tutorials In Hindi
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
JavaScript में comma (,) operator अपने प्रत्येक operand(बाएं से दाएं) को evaluate करता है और last operand को return
करता है।
for example
let x = 1;
x = (x++, x);
console.log(x);
// now use values directly.
x = (2, 3);
console.log(x);
2 3
Description :
Example में अगर आप देखेंगे तो पहले x
की value को 1 से increment किया गया है और , जैसा कि अभी आपने पढ़ा कि comma operator हमेशा last operand return करता है।
अब चूंकि x
की value increase हो गयी है इसलिए output में आपको 2
मिला।
ठीक इसी तरह से जब number को directly use किया गया तो last value output में मिली।
हालाँकि जरूरी नहीं है कि आप 2 ही variables / values use में लें , आप अपनी need के according multiple variables / values को use में ले सकते हैं but , output में हमेशा last value ही मिलेगी।
for example
let x = 1;
x = (x++, x++, x++, x++, x++, x);
console.log(x); // 6
comma operator को आप वहां पर use में ले सकते हैं जहाँ पर आपको किसी single statement में multiple expressions की जरूरत हो। किसी for loop में multiple updaters supply करने के लिए आप इसका use कर सकते हैं।
see below example
// function to get random numbers.
function num() {
return Math.floor(Math.random() * 100);
}
// create 10×10 array of random numbers
const arr = Array.from({ length: 10 }, () =>
Array.from({ length: 10 }, num),
);
// now iterate using comma operator.
for (let i = 0, j = 9; i <= 9; i++, j--) {
console.log(`arr[${i}][${j}] = ${arr[i][j]}`);
}
Output
arr[0][9] = 43 arr[1][8] = 10 arr[2][7] = 62 arr[3][6] = 74 arr[4][5] = 84 arr[5][4] = 34 arr[6][3] = 64 arr[7][2] = 17 arr[8][1] = 89 arr[9][0] = 46
Comma operator का use आप multiple assignment expressions में भी कर सकते हैं।
let a, b, c, res;
res = (a = b = 3, c = 4);
console.log(`a : ${a}`);
console.log(`b : ${b}`);
console.log(`c : ${c}`);
console.log(`res : ${res}`);
output
a : 3 b : 3 c : 4 res : 4
example में , a = b = 3
expression में पहले 3 , b को assign फिर b , a को जिसका मतलब है कि a
और b
दोनों में same value है , लेकिन जैसा कि आपने ऊपर पढ़ा कि comma operator में last operand ही return होगा इसलिए , उससे पहले के सभी expression evaluate होने के बाद res
में 4 मिला।
आपको JavaScript में comma operator के बारे में समझ आ गया होगा।
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook