Prompts / Coding & Software Engineering

ChatGPT prompt for a JavaScript function

This prompt turns a function requirement into code with explicit inputs, outputs, error behavior, and tests. Use it when you want a reviewable implementation instead of a plausible-looking snippet.

Quick answer: This page gives you a free, copy-and-paste Coding & Software Engineering prompt for ChatGPT or Claude. This prompt turns a function requirement into code with explicit inputs, outputs, error behavior, and tests. Enter your email to see the full prompt, then copy it, replace the 7 placeholders in square brackets with your details and paste it into ChatGPT or Claude.

Updated

PromptOpen ChatGPTOpen Claude
Write a production-ready JavaScript function for this requirement: [FUNCTION REQUIREMENT]. It will run in [RUNTIME AND VERSION], using [LANGUAGE MODE AND DEPENDENCIES]. The function signature, if already known, is [FUNCTION SIGNATURE]. Here are representative inputs and expected results: [EXAMPLES AND EXPECTED OUTPUTS]. These are the constraints or policies it must follow: [CONSTRAINTS AND ERROR POLICY].

Start by restating the contract in a compact table: parameter, allowed type or shape, returned value, mutation behavior, and failure behavior. If my requirement is ambiguous, ask up to 3 clarifying questions only if a required input is missing; otherwise state a narrow, explicit assumption.

Then provide:
1. The complete implementation in one JavaScript code block, with JSDoc if the public contract benefits from it.
2. A short explanation of the algorithm, complexity, and any input normalization.
3. A test block using [TEST FRAMEWORK OR STYLE], covering the supplied examples plus empty values, boundary values, malformed inputs, duplicate or ordering behavior where relevant, and mutation checks for arrays or objects.
4. A list of assumptions and integration notes.

Use built-in APIs unless I explicitly allow a dependency. Do not fabricate package APIs, browser support, security guarantees, or benchmark results. Match the stated runtime; do not use syntax unavailable there. Prefer a pure function unless the requirement requires side effects, and make side effects explicit.

Before answering, trace the code against every expected result and check that error behavior is consistent. Flag any behavior you cannot determine from my specification rather than guessing.

Fill in

PlaceholderWhat to enterExample
[FUNCTION REQUIREMENT]Describe exactly what the function must do and any business rules it must apply.Convert an array of cart items into a subtotal by multiplying quantity by unitPrice, excluding canceled items, and rounding only the final total to cents.
[RUNTIME AND VERSION]State where the code runs, such as Node.js 20, a browser target, or a framework version.Node.js 20
[LANGUAGE MODE AND DEPENDENCIES]State JavaScript or TypeScript usage and list allowed libraries, if any.JavaScript with no dependencies
[FUNCTION SIGNATURE]Provide the desired function name and parameters, or say that it should be proposed.calculateSubtotal(items)
[EXAMPLES AND EXPECTED OUTPUTS]Give a few concrete inputs and exact expected outputs or errors.[{quantity:2,unitPrice:19.995,status:'active'}] returns 39.99; [] returns 0; a canceled item contributes 0.
[CONSTRAINTS AND ERROR POLICY]List performance, security, formatting, mutation, and invalid-input requirements.Do not mutate items. Reject non-array input and items with non-finite quantity or unitPrice using TypeError.
[TEST FRAMEWORK OR STYLE]Name the test runner or request plain Node assertions.node:test and node:assert/strict

How to use

  1. Paste the prompt with actual examples from the caller of the function, including one invalid input.
  2. Run the generated tests in the same runtime and lint configuration as your project.
  3. Check the signature, mutation behavior, and error type against existing project conventions.
  4. Follow up with: “Now review this implementation against our existing call sites: [PASTE CALL SITES].”

Variations

Bug fix

Use this when an existing function fails for a known case.

Variation
Debug this JavaScript function in [RUNTIME]: [CODE]. The observed input is [FAILING INPUT], expected behavior is [EXPECTED BEHAVIOR], and actual behavior is [ACTUAL BEHAVIOR]. Identify the smallest root cause before changing code. Return a patch, a short explanation, and regression tests in [TEST STYLE]. Preserve the public API unless a breaking change is necessary. Do not invent dependencies or claim a test passed unless you can derive it from the code provided.

Code review

Use this before merging a function written by you or a teammate.

Variation
Review this JavaScript function for correctness, security, runtime compatibility, and maintainability: [CODE]. Its intended contract is [CONTRACT], runtime is [RUNTIME], and relevant callers are [CALL SITES]. Return findings ranked by severity, each tied to a specific line or behavior; then provide only necessary code changes and tests for high-confidence issues. Check coercion, mutation, null handling, asynchronous errors, and resource cleanup when applicable. Do not rewrite stylistically unless it prevents a real issue.

API wrapper

Use this when writing a function that calls an external API.

Variation
Implement a JavaScript wrapper for [API OPERATION] using [HTTP CLIENT OR PLATFORM] in [RUNTIME]. The documented request and response shapes are [API DETAILS]. My function signature is [SIGNATURE], and retry, timeout, authentication, and error requirements are [POLICY]. Return code plus tests that mock transport behavior. Separate network errors, non-success responses, and invalid response bodies. Do not invent endpoint fields or authentication rules; flag missing documentation.

Tips

  • Give exact input and output examples before asking for code; they expose decisions about rounding, ordering, coercion, and missing values.
  • State whether a function may mutate its inputs, because accidental array sorting or object edits often become hard-to-find caller bugs.
  • Ask for tests that express the contract, then run them locally; AI-generated code can look coherent while using an unavailable API or wrong runtime syntax.
  • For money, authorization, or security-sensitive behavior, define the source of truth and error policy with an engineer or relevant owner before implementing.

FAQ

Should I ask for JavaScript or TypeScript?

Use TypeScript if your project has a type-checking pipeline and the type contract is important. JavaScript can still use JSDoc, but runtime validation remains necessary for untrusted input.

Can AI test the function for me?

It can write test cases and trace simple examples, but you should run them in your project. Environment differences and integration assumptions matter.

How do I prevent overengineered code?

Name the runtime, allowed dependencies, performance limit, and desired signature. Ask for the smallest implementation that meets those constraints.

Related prompts

All Coding & Software Engineering prompts →

Get a few prompts like this every week

Something Big is a free AI newsletter read by 70,000+ professionals. About three short emails a week with prompts and tools that work, plus what changed in AI and what to do about it.