Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Writing Functions | Functions
JavaScript Ninja
close
SectionΒ 3. ChapterΒ 2
ninja.js

ninja.js

bookWriting Functions

Swipe to show menu

Parts of a Function

  • Declaration: starts with the function keyword;
  • Name: identifies the function (e.g., ninjaController, lootZigZag).
  • Parameters: variables inside parentheses that accept values when the function is called;
  • Body: the block of code inside {} containing the function's actions;
  • Return value: an optional value returned by the function. If omitted, the result is undefined.

Example

ninja.js

ninja.js

copy
  • ninjaController : controls the ninja by calling lootBox(ninja) twice and moving right four times;
  • lootBox : groups a sequence of moves and sushi pickups into a reusable helper function;
  • ninja parameter: represents the ninja object that provides methods like goRight(), goUp(), and pickSushi().
Task

Swipe to start coding

Solution

Explore other courses in Catalog

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
ninja.js

ninja.js

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt