Some process handle JavaScript function parameter.

Overview

Function parameter handle or paremeter error control

  • Example 1: Just checking if all arguments were passed / defined.

const required = (name) => {
  console.warn(`Parameter is required and its shuld be type of ${name}`);
};
// use of this
function add(a = required("number"), b = required("number")) {
  return a + b;
}

Or


function add(number1, number2) {
  if (arguments.length < 2) {
    console.warn("Missing arguments");
  }
  return number1 + number2;
}

Or


function add(number1, number2) {
  if (typeof number1 === "undefined") {
    console.warn("First argument is missing / not defined");
  }
  if (typeof number2 === "undefined") {
    console.warn("Second argument is missing / not defined");
  }
  return number1 + number2;
}

  • Example 2: make sure each argument is correct type.

function add(number1, number2) {
  if (isNaN(number1)) {
    console.warn("First argument is not a number");
  }
  if (isNaN(number2)) {
    console.warn("Second argument is not a number");
  }
  return number1 + number2;
}

  • Example 3: make sure each argument is defined and of correct type

function add(number1, number2) {
  if (typeof number1 === "undefined" || isNaN(number1)) {
    console.warn("First argument is missing / not defined");
  }
  if (typeof number2 === "undefined" || isNaN(number2)) {
    throw new TypeError("Second argument is missing / not defined");
  }

  return number1 + number2;
}

  • Example 4: create extarnal function for make sure each argument is defined and of correct type

function handleError() {
  for (let i = 0; i < arguments.length || arguments.length == 0; i++) {
    if (arguments[i] === undefined) {
      return "No argument, This is required";
    } else if (typeof arguments[i] != "number") {
      return "Please Give me Number";
    } else if (arguments[i] < 0) {
      return "Please Give Positive Number";
    }
  }
  return "noError";
}

You might also like...

Handle errors like it's 2022 🔮

Handle errors like it's 2022 🔮 Error handling framework that is minimalist yet featureful. Features Minimalist API Custom error types Wrap any error'

Jan 7, 2023

Helper package to handle requests to a jschan api instance.

jschan-api-sdk Helper package to handle requests to a jschan api instance. How to use npm install ussaohelcim/jschan-api-sdk const { jschan } = requir

Jun 30, 2022

Small TS library to type and safely handle `serde` JSON serializations of Rust enums.

rustie-ts TypeScript library with helper types and functions to type-safely handle Rust's serde JSON serialization of Enums. It can also be used stand

Jul 17, 2022

This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my initial appointment date. It doesn't handle rescheduling.

This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my initial appointment date. It doesn't handle rescheduling.

US-visa-appointment-notifier This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my in

Jan 4, 2023

🍣 CLI to handle Private Git Submodules in your Vercel Project

🍣 CLI to handle Private Git Submodules in your Vercel Project

vercel-submodules: The CLI to handle Private Git Submodules in your Vercel Project This project is community-driven and not affiliated with Vercel in

Jan 1, 2023

Angular JWT refresh token with Interceptor, handle token expiration in Angular 14 - Refresh token before expiration example

Angular JWT refresh token with Interceptor, handle token expiration in Angular 14 - Refresh token before expiration example

Angular 14 JWT Refresh Token example with Http Interceptor Implementing Angular 14 Refresh Token before Expiration with Http Interceptor and JWT. You

Nov 30, 2022

A simple handle tap and hold action for Svelte/SvelteKit.

Svelte Tap Hold Minimalistic tap and hold component for Svelte/SvelteKit, see demo here. Installation // Using Yarn to install yarn add --dev svelte-t

Dec 8, 2022

An itty library to handle common time-related things for your API needs.

An itty library to handle common time-related things for your API needs.

Tiny (~500 bytes) time math library for making date handling and TTLs within your APIs beautiful. Features Tiny @ ~500 bytes gzipped total, and tree-s

Jan 2, 2023

ish.ninja is a free online platform to allocate a unique ish.ninja sub handle name for a BlueSky account.

ish.ninja ish.ninja is a free online platform to allocate a unique ish.ninja sub handle name for a BlueSky account. It is built using Next.js, Xata, T

May 9, 2023
Owner
Md. Nazmul Islam
Full-Stack Web developer
Md. Nazmul Islam
RenderIf is a function that receives a validation as a parameter, and if that validation is true, the content passed as children will be displayed. Try it!

RenderIf RenderIf is a function that receives a validation as a parameter, and if that validation is true, the content passed as children will be disp

Oscar Cornejo Aguila 6 Jul 12, 2022
Export AWS SSM Parameter Store values in bulk to .env files

aws-parameter-bulk Utility to read parameters from AWS Systems Manager (SSM) Parameter Store in bulk and output them in environment-file or json forma

Adam Malik 18 Oct 18, 2022
A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil ?? A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Ben Smith 21 Jun 20, 2022
Tries to execute sync/async function, returns a specified default value if the function throws

good-try Tries to execute sync/async function, returns a specified default value if the function throws. Why Why not nice-try with it's 70+ million do

Antonio Stoilkov 14 Dec 8, 2022
Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Jarred Sumner 19 Dec 19, 2022
A simple react project that contain a single page application (SPA) And a simple caculator to make some calculation and there is a section you can see some Math quotes. Ⓜ💯

A simple react project that contain a single page application (SPA) And a simple caculator to make some calculation and there is a section you can see some Math quotes. Ⓜ??

Reem janina 7 May 31, 2022
This Repo Contains projects that demonstrate some concepts / algorithms / implemetation in some form of digital visualisation

Hacktoberfest 2022 OPEN FIRST Pull Request - GET STARTED WITH OPENSOURCE AND WIN SOME AWWSOME SWAGS ?? Contributors of Hacktoberfest 2022 This project

null 5 Nov 7, 2022
A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages (https://developers.cloudflare.com/pub-sub/)

pubsub A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages. This includes: A isValidBrokerRequest helper for au

Cloudflare 18 Dec 4, 2022
A Discord.JS Command Handler to handle commands eaiser

TABLE OF CONTENTS Installation Setup Changing Default Prefix Creating a Command Usage of minArgs and maxArgs Syntax Errors Global Syntax Errors Per Co

AaTherf 3 Jun 8, 2022