🔧 Error Fixes
· 1 min read

TypeError: X Is Not a Function — How to Fix It in JavaScript


TypeError: document.getElementByID is not a function

You’re calling something as a function that isn’t one — usually a typo in the method name or overwriting a function with a value.

Fix 1: Typo in method name

// ❌ Wrong capitalization
document.getElementByID('btn')  // Not a function!

// ✅ Correct
document.getElementById('btn')

Common typos: getElementByIDgetElementById, addEventlisteneraddEventListener, toUppercasetoUpperCase.

Fix 2: Overwriting a function with a value

// ❌ You reassigned the function
let greet = function() { return 'hi'; };
greet = 'hello';
greet();  // TypeError: greet is not a function

// ✅ Don't reassign
const greet = function() { return 'hi'; };

Fix 3: Calling a callback that wasn’t passed

function fetchData(url, callback) {
  // ❌ callback might be undefined
  callback(data);

  // ✅ Check first
  if (typeof callback === 'function') {
    callback(data);
  }
}

Fix 4: Importing wrong

// ❌ Default vs named import
import { useState } from 'react';  // ✅ Correct
import useState from 'react';       // ❌ Wrong — not a default export