JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – is the console log function really override

JavaScript Quiz – is the console log function really override

Console log function

In javascript, console.log() is a function that writes a message to log on the debugging console. You will not able to see anything on the screen. But you can also override the console log function.

Basic Syntax

console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);

For more detail about the javascript console log click here.

JavaScript Question

In this javascript quiz, find out is the console log function really override because it change the behaviour of the function.

First, we declare a console log function and javascript string pass to it and string is “Hi, JS”.

console.log("Hi, JS");
// output => "Hi, JS"

As, you see in the above statement it will print the “Hi, JS” in the debugging web console.

As, console is an object and log the method of console. So, we pass a new function to log. But no function body is defined.

console.log = function(){};

Now, we console log again with the javascript string “Hi, Startup”.

console.log("Hi, Startup");

Finally, Any idea what should be the output of above statement ?

JavaScript Quiz Test

Here is the full code snippet.

console.log("Hi, JS");
// output => "Hi, JS"

console.log = function(){};

console.log("Hi, Startup"); // output = > ?

Answer – Blank

Click To Run Code

Explanation

Now, you know the answer is “Blank“. But why doesn’t it should give “Hi, Startup”. Let me explain.

As, it’s a default behaviour of console log to print the value in the web console. But we can override the default behaviour by passing our own code.

As we change the console log functionality and do not define anything in it.

At last, That’s why we get an answer “Blank“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – does object hasOwnProperty really return boolean
  2. JavaScript Quiz – does class typeof operator give class
  3. JavaScript Quiz – array map converts an element value with parseInt
  4. JS Quiz – does the splice method replace array element

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link