Tristan Tran
Tristan Tran

Reputation: 1513

Window object with Javascript in VS Code

I am new to Javascript and trying out this code from a book. It says the window object is to be displayed with a browser. I have Node.js installed. But when I run the code below (with F5), I get this message:

Process exited with code 1
Uncaught ReferenceError: alert is not defined

Here is the code:

var age = 29;
var sayAge = () => alert(this.age);
alert(window.age);
sayAge();
window.sayAge();

How do I go about seeing what this code output is?

Upvotes: 1

Views: 2412

Answers (2)

Antonio Pinto
Antonio Pinto

Reputation: 11

That error means that your code has no idea where that alert method is from. In order to make use of that alert i use html. If u dont know how to code html i suggest u take a look because is very handy. Follows an example from w3schools.com I suggest u use Visual Studio Code if you are working on a project, or just edit from w3schools or a similar website.

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Button Text!</button>

<script>
function myFunction() {
  alert("Alert!");
}
</script>

</body>
</html>

Upvotes: 0

andy mccullough
andy mccullough

Reputation: 9561

Use console.log() instead. Alert is for a popup type alert in a browser.

console.log() can be used in either the browser or NodeJS

E.g. console.log(age)

It will either log to the NodeJS console that's running your application, or the browser console if you're using it in a browser environment

Upvotes: 2

Related Questions