CodeRef
CodeRef
  • Software
    • VSCode
  • Midjourney_AI
    • Midjourney Cheat Sheet
  • aws
    • S3 Reference
    • Services
  • bash
    • .bashrc File Contents
    • CAN
    • _System Config File Locations
    • argument-length-limits
    • Conditionals
    • Data Structures
    • File Permissions
    • File Syncing
    • File System
    • Functions
    • General
    • Loops
    • My Functions
    • Networking
    • Number Operations
    • OpenVPN
    • Operators
    • Resource Management
    • Serial RS232
    • Spinning Wheel Animation
    • SSH
    • Text Operations
    • Environment Variables
  • cpp
    • ChatGPT | Pointers vs. References
    • arrays
    • Classes
    • Data Types / Structures
    • Enumerated Classes
    • Exception Handling
    • Function Objects
    • Functions
    • I/O
    • Loops
    • Macros
    • Namespaces
    • New Features
    • Pointers
    • Scope
    • Smart Pointers
    • Raw String Literals
    • Style Guide
    • Switch Case
    • Templating
    • How to Use tinyxml2
    • Useful Libraries
    • google-test
    • Conditionals
    • Rule of Three/Five
    • Optional Parameters
    • Keywords
    • Filesystem
    • Random
    • Casting
    • tools
  • git
    • Code Review Dependency Strategy
    • Git Bisect Guide
    • Git Reference
    • removing-cherry-picks
    • Useful Tools
    • Graphite Reference
  • js
    • functions
    • Javascript Reference
  • linux
    • Display
    • Dual Boot with Windows
    • File System
    • NVIDIA
    • Sending/Receiving TCP/UDP Messages in Ubuntu
    • dynamically_linked_binaries
  • markdown
    • Images
    • obsidian-reference
  • python
    • Classes
    • Exceptions
    • Functions
    • Operations
    • Python Reference
    • unittest_command-line-args
    • unittest_magicmock_GPT
    • unittest_mock
    • unittest_printing
    • unittest_subtest
    • useful-stuff
    • jupyter
    • poetry
  • ros
    • _ROS Cheat Sheet
    • Create New Workspace
    • Install ROS
    • Node Sample - Listener
    • Node Sample - Talker
    • Node Template
    • Setup
    • urdf
  • excel
    • excel-reference
  • windows
    • File System
    • WSL - Windows Subsystem for Linux
    • WSL
  • software_engineering
    • uncle_bob_lectures
      • Overview
      • Lesson 01 - Notes
  • web
    • Front End
    • Hugo
    • new_frontend_tools
  • sql
    • cheatsheet
Powered by GitBook
On this page
  1. js

functions

From ChatGPT.

In JavaScript, there are several ways to define functions. Here are the main methods:

  1. Function Declaration:

    • This is the most common way to define a function. It involves using the function keyword, followed by the function name and parameters.

    function add(a, b) {
        return a + b;
    }
    • Function declarations are hoisted, meaning you can use them before they are declared in the code.

  2. Function Expression:

    • In a function expression, a function is assigned to a variable. This can be done using the function keyword or, more commonly, using the arrow function syntax.

    // Using function keyword
    const multiply = function(a, b) {
        return a * b;
    };
    
    // Using arrow function
    const divide = (a, b) => {
        return a / b;
    };
    • Function expressions are not hoisted like function declarations.

  3. Arrow Functions:

    • Arrow functions provide a more concise syntax, especially for small anonymous functions.

    const square = (x) => x * x;
    • Arrow functions do not have their own this value; they inherit it from the surrounding scope.

    • Arrow functions are the most common syntax for callback functions

    • Arrow functions are synonymous with callback functions, and are essentially lambda functions

  4. Function Constructor:

    • You can create a function using the Function constructor. This is less common and not recommended due to security risks (it can execute arbitrary code passed as a string).

    const greet = new Function('name', 'console.log("Hello, " + name);');
    
    greet('John');
    • Using the Function constructor is discouraged because of security implications and the potential for injection attacks.

  5. Generator Functions:

    • Generator functions are special kinds of functions that can be paused and resumed.

    function* generateSequence() {
        yield 1;
        yield 2;
        yield 3;
    }
    
    const generator = generateSequence();
    
    console.log(generator.next().value); // 1
    console.log(generator.next().value); // 2
    • Generators are denoted by the function* syntax.

  6. Export using ES Modules (modern method):

    • Makes a function available to external modules importing this module.

    // myModule.js file
    export function add(a, b) {
        return a + b;
    }
    
    // In another file
    import { add } from './myModule.js';
    
    add();

These are the main ways to define functions in JavaScript. Each has its use case, and the choice often depends on factors like readability, scoping requirements, and the need for certain features (like arrow functions for concise syntax or generator functions for asynchronous programming).

Last updated 11 months ago