The error “E.split is not a function” means you are calling .split() on a value that is not a string — usually a number, an object, undefined, or null. To fix it, convert the value to a string first with String(value) or value.toString(), or check its type before calling .split().
Why This Error Happens
JavaScript’s String.prototype.split() method exists only on strings. When you write someValue.split(','), the JavaScript engine looks for a property called split on that value. If the value is not a string, that property is usually undefined, so calling it as a function throws TypeError: someValue.split is not a function.
Common triggers:
- Reading from an input field without converting:
document.getElementById('num').valueactually returns a string, but if you useinnerTextor a number variable, you might get a number. - Parsing JSON and expecting a string, but the field is
nullor an object. - Using
spliton an array element that is a number (e.g., from a CSV file where numbers are not quoted). - Calling
splitonundefinedbecause a variable was not assigned.
Example that fails:
let id = 12345;
let parts = id.split(''); // TypeError: id.split is not a function
How to Fix It: Convert to String
The most direct fix is to ensure the value is a string before calling split(). Use String(value) which handles null and undefined gracefully (returns "null" or "undefined"), or value.toString() which throws on null and undefined.
Corrected example:
let id = 12345;
let parts = String(id).split(''); // ["1","2","3","4","5"]
If you are dealing with user input or API responses, you often want to guard against non-string values:
let input = someObject.field; // could be anything
let parts = (typeof input === 'string') ? input.split(',') : [];
For arrays, you can use Array.prototype.join() first:
let arr = [1,2,3];
let parts = arr.join(',').split(','); // ["1","2","3"]
Common Situations and Specific Fixes
Different contexts produce this error in slightly different ways. Here are the most frequent real-world cases:
- Reading a number from an input field: Use
String(input.value)— even though.valueis usually a string, if you usedparseIntorNumber()earlier, you have a number. - Working with JSON fetched from an API: Check the field type. For example,
data.tagsmight be an array, not a comma-separated string. UseArray.isArray(data.tags)and thendata.tags.join(','). - Using
splitonthisinside a callback: If you lose the context,thismight beundefined. Use an arrow function or bind. - In Node.js reading a file:
fs.readFileSyncreturns a Buffer unless you specify encoding. Convert withbuffer.toString().split('\n').
How to Debug It Yourself
When you see this error, the fastest way to find the culprit is to add a console.log right before the failing line:
console.log('value:', value, 'type:', typeof value);
let result = value.split(',');
Look at the console output. If it says type: number, convert it. If it says type: object, check if it’s an array or a plain object. If it says type: undefined, trace where the variable was supposed to be assigned.
You can also use the debugger in DevTools (F12 on Windows, Cmd+Opt+I on Mac) and set a breakpoint on the line. Hover over the variable to see its value and type.
Related String Methods You Might Need
Once you fix the type issue, you might want to use other string methods that have similar pitfalls:
slice()— works on strings and arrays, but if you call it on a number, you get the same type error.includes()— checks for a substring; again, only on strings (and arrays).replace()— only on strings; if you have a number, convert first.trim()— only on strings; often used after reading input.
For splitting a string into an array of characters, you can also use the spread operator: [...'hello'] gives ['h','e','l','l','o']. This works only on strings (and iterables), so the same conversion rule applies.
Preventing This Error in Future Code
Good habits reduce the chance of hitting this error:
- Always validate or coerce types when reading external data (form inputs, API responses, file contents).
- Use defensive coding:
const str = (value == null) ? '' : String(value); - If you use TypeScript, annotate variables as
stringto catch this at compile time. - For array of strings, use
Array.isArray()before calling array methods.
Remember that split() does not modify the original string; it returns a new array. If you need to split a string that is stored in a variable, reassign the result if you want to keep it.
Frequently Asked Questions
Why does "123".split() work but 123.split() doesn’t?
Because "123" is a string literal, and strings have the split method. 123 is a number literal; numbers do not have a split method. JavaScript does not automatically convert numbers to strings for method calls — you must do it explicitly.
Can I use toString() on null or undefined?
No. Calling null.toString() or undefined.toString() throws a TypeError. Use String(null) which returns "null" and String(undefined) which returns "undefined". If you want an empty string for those, use a conditional.
Does this error happen in Node.js too?
Yes, the error is identical in Node.js. Common in Node when reading from process.argv (which is an array of strings, so that’s fine) or when parsing environment variables that might be numbers if you used parseInt.
What if I’m using a framework like React or Vue?
The error is the same. In React, it often happens when you pass a number as a prop and try to split it. In Vue, it might happen in computed properties. The fix is the same: convert to string before splitting.
Is there a way to split a number without converting to string?
You can use arithmetic to extract digits, but it’s more complex. For example, to get digits of a positive integer: const digits = []; while (num > 0) { digits.unshift(num % 10); num = Math.floor(num / 10); }. But for most cases, converting to string is simpler and clearer.
Does split() work the same in all browsers?
Yes, split() is part of ECMAScript 3 and is supported in all modern browsers and Node.js. The error message might vary slightly (e.g., “is not a function” vs “is not a function”), but the cause and fix are the same.
Related
- Computer Data Entry Operator: Job, Skills, Pay
- Cordless Charging Bedside Table: How It Works
- Google Sheets QR Code: How to Make & Use Them
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Errors & fixes
- Excel Not Highlighting Columns? Fix Selection IssuesIf Excel is not highlighting a column, click its lettered heading at the top of the worksheet, or…
- Excel NPV Errors: Why Results Look Wrong and How to Fix ThemExcel NPV errors most often come from including the initial investment in the wrong place, using a discount…
- Excel Power Query Won’t Refresh? Fixes That WorkIf your Excel Power Query won’t refresh, the cause is almost always one of three things: a changed…
- Excel File Won’t Open: How to Repair ItIf an Excel file will not open, first make a copy of it, then use Excel for Microsoft…
- Excel Circular Reference Warning: Find and Fix ItExcel’s circular reference warning means a formula depends on its own result, either directly or through other cells.…
- Excel Filter Not Working in SharePoint? Causes and FixesIf a filter is not working in Excel in SharePoint, first check that the filter covers the full…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.