Why javascript code is not working?

When I write in my javascript file this code:

var total = 5+4;
console.log(total);

But when I write:
var total = 5+4;
alert(total);

It is working.

Why the first one is not working?

Be aware - console doesn’t create a pop-up, so you need to look at the console to get it to work…

I did this quick and dirty (changing the math to show the difference).

<html>
<head>
	<script>
		var total = 5+15;
		console.log(total);
	</script>
	<script>
		var total = 5+4;
		alert(total);
	</script>
</head>
<body>
	<p>Testing console vs alert</p>
<script>
</body>
</html>

and got this result. You see the alert, but to get to the console.log, you need to go into the developer tools…

Where are you looking for the console data?

If you are using the Firefox browser, then you’ll see the console output when you click on the 3-bar icon at upper right, Then on Developer, then on Web Console. Your console output will be found there. Just refresh the page and watch it appear.

2 Likes

Thank you!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.