Scroll
Javascript урок 4 Функции
In this lesson, we will look at functions in JavaScript—how to write them and what kinds of functions exist. You can read about functions in these PHP lessons; all the examples will also work in JavaScript:
PHP Lessons – Lesson 7 – PHP Functions and Functional Programming
PHP Lessons – Lesson 9 – Recursion
The syntax is the same as in PHP, but there's one difference. In JavaScript, all variables are global by default, and they work in all nested functions if declared before the function is called. For example, the following code behaves differently in PHP and JavaScript.
PHP:
<?php function inc(){ $x++; return $x; } $x = 10; inc($x); print $x; ?>
JavaScript:
<script> function inc(){ x++; return x; } x = 10; inc(x); alert(x); </script>