Click here to Skip to main content
15,919,423 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
In this code, I was just wondering why the function doesn't have a name, and why does it end with a semicolon }; instead of just a curly bracket }

window.onload = function() {
    var x = document.getElementById("demo");
    x.style.color = '#6600FF';
    x.style.width = '100px';
};


What I have tried:

I also tried adding a name to the function, and it gave the same answer so I'm guessing it's optional?
Posted
Updated 3-Dec-17 5:19am

1 solution

It's called an "anonymous" function[^]. Since it's being assigned to an onload event, this is possible.

The semicolon needs to be there because this is an assignment statement, not really the definition of a function. All assignment statements must end with a semicolon.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900