JavaScript console Object : Window Console Object In Hindi

JS console Object In Hindi

Hello Learners ! अगर आप JavaScript use करते हैं तो अपने console.log() method तो जरूर use किया होगा , लेकिन क्या आप जानते हैं console Object window Object की ही एक property है और log() की तरह ही इसमें कई और methods भी हैं।

हालाँकि कई developers console Object को नजरअंदाज कर देते हैं , लेकिन कोई नहीं इस article में हम console Object और इसके useful methods के बारे में बात करेंगे।

Image could not load

Image on pixabay

window console Object

JavaScript में console object , window Object की ही एक property है। जो हमें browser के debugging console का access provide करता है जिसकी help से हम need के according console पर message , error या info write / clear कर सकें।

console object के सभी methods को access करने के लिए आप window.console या सिर्फ console use कर सकते हैं क्योंकि window Object तो globally available रहता ही है।

console Object's Methods

conosle Object के कुछ useful methods इस प्रकार हैं -

  1. log()

  2. info()

  3. error()

  4. warn()

  5. table()

  6. group()

  7. clear()

  8. trace()

  9. assert()

1. console log()

इस method की help से किसी message को console पर log (write) करने के लिए किया जाता है , बैसे किसी developer द्वारा इस methods को JavaScript में सबसे ज्यादा बार use किया जाता है।

let obj = {fname:"Rahul", ltname:"Rajput"}; console.log(obj);

console message देखने के लिए browser console open करना न भूले। browser console को right click करके inspect पर click करके या F12 press करके open कर सकते हैं।

2. console info()

info() method की help से किसी message को console पर write करने के लिए किया जाता है , जब आप info() method use करते हो तो message के starting में एक i icon देखने को मिलेगा , जो info को indicate करता है।

console.info("This is info message");

3. console error()

error() method का use console पर errors write करने के लिए किया जाता है , यह method testing purposes के लिए काफी useful है।

console.error("Oh ! no , you just did something wrong");

4. console warn()

warn() method का use console पर warning write करने के लिए किया जाता है । , जब आप warn() method use करते हो तो message के starting में एक warning icon देखने को मिलेगा।

console.warn("Do not run it again ..."); console.warn(["you", "can", "print", "anything"]);

ध्यान रहे , Browser में logs / errors या warning को enable करना पड़ेगा तभी आपको ये messages दिखेंगे।

5. console table()

debug करने के लिए ये भी एक अच्छा method है , table() method का use console पर Array या Object को table की form में write करने के लिए किया जाता है ।

// print array. console.table(["Raju", "Ram", "Rahul"]); // print Object. console.table({fname:"Rahul", lname : "Kumar", address:"UP, India"});

हालाँकि , table() method में अगर आप कोई string / number pass कर रहे हैं तो normal message की तरह ही print होगा।

6. console clear()

इस method के name से आप समझ सकते हैं कि इसका use console clear करने के लिए किया जाता है ।

console.log("log"); console.info("info"); console.warn("warning"); console.error("error"); console.clear("Cleared");

7. console assert()

अगर आपको किसी condition के according console message print कराना है , तो आप assert() method का use कर सकते हैं। ये method argument accept करता है , पहला boolean value : true | false और दूसरा message . condition गलत होने पर message print होगा।

console.assert(false, "It will print definitely."); //or let x = 90, y = 20; console.assert(x == y, "x, y are not equal.");

8. console group()

group() method का use console पर messages की grouping करने के लिए किया जाता है। इस method को endGroup() method के साथ use किया जाता है।

Messages की grouping करने के लिए हर group को एक name देना होता है , जिसे label कहते हैं। label को आपको group() में pass करना पड़ता है।

// group 1 console.group("Group 1"); console.log("message inside group 1"); console.log("another message inside group 1"); console.groupEnd(); // group 2 console.group("Group 2"); console.log("message inside group 2"); console.log("another message inside group 2"); console.groupEnd();

9. console time()

time() method का use console view पर timer start करने के लिए किया जाता है। performance testing के हिसाब से काफी useful method है। इस method को endTime() method के साथ use किया जाता है।

इस method को use करने के लिए time() और endTime() दोनों में same label pass करना पड़ता है।

Example के लिए नीचे example में यह check किया गया है कि for Loop और while Loop में कौन सा ज्यादा fast है -

console.time("for Loop"); for (let i = 0; i < 5000000; i++) ; console.timeEnd("for Loop"); let i = 0; console.time("while Loop"); while (i < 5000000) i++; console.timeEnd("while Loop");
Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

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