JavaScript Tutorial
  • 🍼Introduction
  • 🥛Hello Gamers
  • 🍯Variables
    • Action Game Example
  • 🌰Strings
    • RPG Example
  • 🍪Character Acces
    • Simulation Example
  • 🍩Plus (+) operator
    • Strategy Game Example
  • 🥜Functions
    • Adventure Game Example
  • 🍿Template strings
    • Strategy Game Example
  • 🍫Numbers
    • Sports Game Example
  • 🍬Converting Numbers to Strings
    • Fighting Game Example
  • 🍭Operations
    • FPS Game Example
  • 🍮Conditions
    • Platformer Game Example
  • 🎂Arrays
    • Puzzle Game Example
  • 🍰Objects
    • Racing Game Example
  • 🧁Arrow Functions
    • Rhythm Game Example
  • 🔌Shameless Plug
  • 🥧Implicit Return
    • Platform Game Example
  • 🍦String Methods
    • GRID Game Example
  • 🍨Object Literals
    • Board Game Example
  • 🍧DynamicProperty
    • Political Game Example
  • 🍡Reading Dynamic Property
    • Hide & Seek Game
  • 🥮Objects Continued
    • Medieval Game Example
  • 🥠Object Shorthands
    • Trivia Game example
  • 🔌Shameless Plug (again)
  • 🍥Destructuring & Concatenation
    • Dig a hole game example
  • 🍏Optional Chaining
    • Action Game Example
  • 🍐Nullish coalescing operator
    • Adventure Game Example
  • 🍊Refactoring If Conditions
    • Arcade Game Example
  • 🍋Implicit Conversion & Falcy Values
    • Strategy Game Example
  • 🍌Arrays of objects
    • Sports Game Example
  • 🍉Transforming Array of Objects
    • Fighters Game Example
  • 🍇Some important array methods
    • FPS Example
  • 🫐Reducing arrays of objects
    • Third Person Shooter Example
  • 🍓Try....catch
    • Platformer Example
  • 🍈Immutability
    • Puzzle game example
  • 🍒Creating new arrays or objects
    • Racing Game Example
  • 🔌Shameless Plug (again)
  • 🍑Immutable object operations
    • Basics
    • Rhythm Game Example
  • 🥭Classes
    • Basics
    • Platformer Game Example
  • 🍍Instance Method
    • Basics
    • Role-playing game example
  • 🥥Object Oriented Programming
    • Basics
    • Stealth Game Example
  • 🥝Getters & Setters
    • Basics
    • Survival Game Example
  • 🍅Static Methods & Chaining
    • Basics
    • Tactical Role Playing Game
  • 🍆Class Inheritance
    • Tower Defense Game Example
  • 🥑Use of Super
    • Vehicular Combat Game example
  • 🫒Public Class Field
    • MMORPG Example
  • 🥦Private class fields
    • Battle Royale Games Example
  • 🥬Aysnc/Await
    • Collectible Card Game example
    • Video Board game example
  • 🫑SetTimeout
    • Dice game example
  • 🥒Execution Order
    • Digital Collectible Card Game example
  • 🌶️The callback pattern
    • Life Simulation Game example
  • 🌽Promises
    • Music Game Example
  • 🥕Writing a function that returns a promise
    • Party game example
  • 🧄Introduction to JSON
    • Tile based game example
  • 🧅JSON.parse(string) / JSON.stringify(object)
    • Rhythm action game example
  • 🍠JSON Example for a game
    • Fetching JSON game data
  • 🥐Handling fetch errors
    • Fetch and HTTP Requests
  • 🥯ES Modules
    • Tower Defence Game Example
  • 🍞Default Export
    • Strategy Adventure Game Example
  • 🥖Fetch Wrapper
    • Strategy Role-playing game example
  • 🔌Final Shameless Plug
  • 🫓Lexical Scope and Arrow Functions
    • Vehicular Combat Game Example
  • 🥨Passing Functions
    • Vehicular simulation game example
  • 🥚Dynamic Imports
    • Video card game example
  • 🍠What is a closure
    • Video casino game example
  • 🎆Congratulations
Powered by GitBook
On this page
  1. Implicit Return

Platform Game Example

Let's say we are creating a platform game where the player can collect coins and power-ups. We want to create a function that checks if the player is currently standing on a coin or a power-up, and returns the corresponding value.

let player = { x: 10, y: 20 };
let coins = [{ x: 5, y: 25 }, { x: 12, y: 20 }, { x: 15, y: 30 }];
let powerUps = [{ x: 8, y: 22 }, { x: 18, y: 25 }, { x: 20, y: 15 }];

// Using explicit return
function checkCollectible() {
  for (let i = 0; i < coins.length; i++) {
    if (player.x === coins[i].x && player.y === coins[i].y) {
      return "coin";
    }
  }
  for (let i = 0; i < powerUps.length; i++) {
    if (player.x === powerUps[i].x && player.y === powerUps[i].y) {
      return "power-up";
    }
  }
  return "none";
}

let collectible = checkCollectible();
console.log(collectible); // Output: "coin"

We can use implicit return to make the code more concise, by using an arrow function and returning the value using the shorthand syntax:

let checkCollectible = () => 
  coins.find(coin => player.x === coin.x && player.y === coin.y) ? "coin" :
  powerUps.find(powerUp => player.x === powerUp.x && player.y === powerUp.y) ? "power-up" : "none";

console.log(checkCollectible()); // Output: "coin"

In this example, the function checkCollectible is an arrow function that check if the player is standing on a coin by using the find() method of the coins array, if the find method returns a value, it returns "coin", otherwise, it check for a power-up by using the find() method of the powerUps array, if the find method returns a value, it returns "power-up" otherwise it returns "none".

It is important to note that the find() method returns the value of the first element in the array that satisfies the provided testing function, and ? : operator is a shorthand of a if else statement that return the first value if the statement is true, otherwise returns the second value.

In summary, implicit return can make the code more concise by using arrow functions and shorthand syntax, it can help to better readability of the code, make it more maintainable, and ease the understanding of the game logic.

PreviousImplicit ReturnNextString Methods

Last updated 2 years ago

🥧