0
How does Console.log help in debugging? Can you give me an example too?
I was curious to know what us the difference between console.log and document.write so I searched it and found this post https://www.sololearn.com/discuss/262481/?ref=app here ppl have mentioned that they use it for debugging so how do they actually do it?
2 Answers
+ 3
Imagine this scenerio:
*This is in c#, but its the same thing for Javascript*
private int playerHp;
void playerDie(){
if(playerHp <= 0)
die();
}
Hm.. this isn't working.
So I do:
Console.Write(playerHp);
Output: 100.
Maybe I realize:
The Hp never went down?! Ohhh I forgot to do
playerHp -= enemyDmg;
When he was supposed to take damage.
So, it allows you to see values just like a debugger to help you narrow down the problem.
You could use alert() too for Javascript.
I do this kind of thing all the time for debugging. Very useful if you're stuck on a bug.
0
Check out this unit test method:
if function return value equals to expected result, console.log(" test has passed.");
if function return value not equals to expected result, console.log(" test has failed.");
https://code.sololearn.com/W2n6kfeQixs1/#html