JavaScript: Why do I have to create a var and a function for a .onclick event when I only want it to create a function?

Asked by Last Modified  

Follow 2
Answer

Please enter your answer

IT Professional Trainer with 15 years of experience in IT Industry

You don’t. You can pass an anonymous function to the onClick. object.onclick = e => { // handle event here...};or object.addEventListener(“click”, e => { // handle event here...});
Comments

IT Professional Trainer with 15 years of experience in IT Industry

Your question is not clear. But I will give you an answer to your question. var is initializer ob variables / HTML elements object. When you are want to create an event function that time you needs some elements, objects will be works in your function. So you need to create var for init for your elem...
read more
Your question is not clear. But I will give you an answer to your question. var is initializer ob variables / HTML elements object. When you are want to create an event function that time you needs some elements, objects will be works in your function. So you need to create var for init for your elements. read less
Comments

Use of var means variable valid for only current execution context or local context or the variable is local.
Comments

View 1 more Answers

Related Questions

What's the event loop?
In computer science, the event loop is a programming construct that waits for and dispatches events or messages in a program. It works by making a request to some internal or external "event provider"....
Spandana
0 0
5
How to start Learning Javascript, Jquery and Angular
Very easy, your requirement in advanced client side technologies, you can get this course every where
Sandeep
x eskor plus two x plus one
what is the question? can't understand the meaning.
Ankur
0 0
8

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

What Should I Learn in 2025 for Frontend or Web Technology?
If you're wondering what to focus on in 2025 to excel in frontend development, here's a concise guide: 1. React.js React remains one of the most in-demand technologies for building modern web applications....

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...

Benefits of Design Patterns in Application Development
Application developments is a tedious job. Programmers write code and test it again and again to make sure their software works fine. So designing complex software is a hard nut to crack. Skilled programmers...

Java Interview Questions & Answers
Java Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Java Programming Language. As per my...

Closures in Javascript
A closure is the combination of a function and the lexical environment within which that function was declared. Consider the following example: function makeFunc() { var name = 'Mozilla'; function...

Recommended Articles

Here are the top 6 must have skills for every Web Designer : Technical Skills Web designers undoubtedly require very strong technical skills. They must have hands on using Web designing software and tools, such as Adobe Dreamweaver, Fireworks, Photoshop, Flash etc. In addition to the knowledge of design...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Looking for Java Script Training classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you