• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do JavaScript function work

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning JavaScript in my beginning programming course. It looks interesting so far but I would like to know, if there is any easy way to understand how function works.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're just starting to learn a language, it's really of no use at all to know "how things work". Just accept that they work and carry on with learning the language.

Unless I'm not understanding your question?
 
Zulikha Shafi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for replaying...
my quastion was more of how to code using a function in js. someone sugested me to watch a tuitarial on youtube but want to get a simple example. Thank you again!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Functions in JavaScript are a deep topic, but understanding functions is key to writing good and effective JavaScript.

At its most basic level, you declare a named function and then you can call it within its scope.

For example:
declares a function. It can be called anywhere within its scope as follows:

The rabbit hole goes much deeper but that should get you started.
 
Greenhorn
Posts: 20
jQuery Python Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript is such a weird language when it comes to how functions are called/initiated. DOM is a big part of Javascript, because it's often used in reference to HTML elements, and you can call a function in JavaScript, or with an action event in HTML. It really depends on what you want each function to do in your program.

A very common one is body onload



For instance, you might want Javascript code to simply do something when you press a button.

Then you can create a button in HTML and do this:

But what if you want to be able to change something every time you click the button, like, make something move or get larger? Then you might need to put the click event in javascript:




Sometimes you want a function to by triggered by another function.



There are also times when you want to just call a function





I also wanted to state that I wouldn't recommond JavaScript as a first language, as it might confuse you when you learn other programming languages. In my personal opinion, if I had it to do over again, I would have gotten a good grasp of Python and maybe C/C++ before moving on to other languages. It's important to have a decent understanding of functional and procedural programming, and it can help you to think more algorithmically. But...that's just my humble opinion.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Katherine Mejia wrote:JavaScript is such a weird language when it comes to how functions are called/initiated.



I would say "varied and deep" rather than weird.

DOM is a big part of Javascript


Not true, or at best, misleading. The DOM is not part of the JavaScript language. It's part of HTML (and that's simplifying things, but will do for now). The browsers provide JavaScript bindings for a DOM API, but that doesn't make the DOM part of JavaScript.

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course if you really want the skinny on functions, I (of course ) suggest you pick up a copy of Secrets of the JavaScript Ninja and read Part II. You'll never look at functions the same way again.
 
Katherine Mejia
Greenhorn
Posts: 20
jQuery Python Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Katherine Mejia wrote:JavaScript is such a weird language when it comes to how functions are called/initiated.



I would say "varied and deep" rather than weird.

DOM is a big part of Javascript


Not true, or at best, misleading. The DOM is not part of the JavaScript language. It's part of HTML (and that's simplifying things, but will do for now). The browsers provide JavaScript bindings for a DOM API, but that doesn't make the DOM part of JavaScript.


True. Good points. I should have stated it better. I'm now really exploring the various depths of JavaScript. I find it to be a fascinating language, so far.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic