Guides for Code Editor
Debugging JavaScript on iPhone: using a console
On a desktop you press a key and the browser's developer tools open. On an iPhone there is no such panel next to the page, so many people end up guessing why a button does nothing. There are two ways to get a console: connect the iPhone to a Mac and use Safari's Web Inspector, or use an editor app with a console built in.
This guide covers both, the console methods you'll use every day, the error messages you'll see most often on Apple devices and how to read them.
Why you need a console on iPhone
When JavaScript hits an error, it stops running that piece of code – and the page simply looks frozen. The console is where that error is reported, including the line where it happened. Without a console you only see the symptom, never the cause.
Code Editor has a console built in: console.log output, warnings and errors with their line number appear right below the live preview, and tapping an error jumps to its line. If you're just starting with web pages, the guide to coding HTML, CSS and JavaScript on iPad walks through a complete example.
Safari's Web Inspector with a Mac
If you have a Mac, Safari can show the console of a page that is open on your iPhone or iPad. Turn on Web Inspector in the iPhone's Safari settings under Advanced, turn on the web developer features in Safari's Advanced settings on the Mac, connect the iPhone and pick it in Safari's Develop menu on the Mac.
That works well for testing a live website, but it ties you to a desk. To write and debug on the iPhone alone, you need an app with its own console.
console.log, console.warn and console.error
These three methods cover most debugging. They all accept several values separated by commas, so you can label what you print.
const items = ["apple", "pear"];
console.log("Items:", items);
console.warn("Only", items.length, "items");
console.error("Could not load prices");- console.log – everyday output: values, checkpoints, “got here” messages
- console.warn – something unexpected that isn't fatal, such as an empty list
- console.error – something went wrong, for example a failed request
A simple debugging routine
Most bugs can be found by checking your assumptions one at a time. Log the value right before the line that misbehaves and compare it with what you expected.
Here is a classic: a total that comes out as text instead of a number. Logging the result shows "053" instead of 8, because one price is the string "5" and the + operator joins strings instead of adding them. Converting with Number(price) fixes it. If you're stuck, an AI coding assistant can explain an error message – paste it in with the line number.
function total(prices) {
let sum = 0;
for (const price of prices) {
sum += price;
}
return sum;
}
console.log(total(["5", 3])); // "053" instead of 8Common error messages on iPhone and iPad
Safari and nearly all other browsers on iPhone and iPad use Apple's JavaScript engine, and its messages read differently from the ones in Chrome tutorials. Here are the ones you'll meet most often, with what they usually mean.
| Message | Typical cause |
|---|---|
| ReferenceError: Can't find variable: totl | A typo in a variable name, or the variable is declared somewhere else |
| TypeError: null is not an object (evaluating 'button.addEventListener') | getElementById or querySelector found nothing – wrong id, or the script runs before the element exists |
| TypeError: undefined is not an object (evaluating 'user.name') | A variable or property hasn't been set yet |
| TypeError: … is not a function | A misspelled method name, such as fliter instead of filter |
| SyntaxError: Unexpected end of script | A missing closing brace or parenthesis |
How to read an error message
An error message has three useful parts. The type (ReferenceError, TypeError, SyntaxError) tells you the category. The text explains what went wrong, often quoting the exact expression in parentheses. And the line number tells you where to look.
Be aware that the line with the error isn't always the line with the mistake. If button is null on line 12, the real problem may be a wrong id in your HTML or a script tag placed in the head before the body exists. Start at the reported line, then trace the value backwards to where it came from.
- SyntaxError: the code couldn't even be read – check brackets, quotes and commas near the line.
- ReferenceError: a name doesn't exist – check spelling and where it's declared.
- TypeError: a value has the wrong type – log the value right before the line.
Try things out in the console prompt
Sometimes you just want to know what an expression returns – what does "5" + 3 give, or how does an array method behave? In Code Editor you can type JavaScript into the console prompt and see the result immediately, with a history like a terminal. That is often faster than editing the file and reloading.
Clean up before you publish
Debug output is meant for you. Before you publish, remove console.log lines that print internal values, keep console.error for real failures and make sure the console is free of errors when you use the page as a visitor would.
Frequently asked questions
Can I see console.log output on iPhone?
Not inside Safari on its own. You need either a Mac with Safari's Web Inspector connected to the iPhone or an app with a built-in console. Code Editor shows console output, warnings and errors directly below the page preview.
How do I open the console in Safari on iPhone?
Safari on iPhone has no console of its own. Turn on Web Inspector in the iPhone's Safari settings under Advanced, connect the iPhone to a Mac and choose it in the Develop menu of Safari on the Mac.
Why does my error message look different from tutorials?
Most tutorials show messages from Chrome. On iPhone and iPad, browsers usually use Apple's JavaScript engine, which words the same errors differently – for example “Can't find variable: x” instead of “x is not defined”.
Why is my element null?
Usually the id in getElementById doesn't match the HTML exactly, or the script runs before the element exists. Place the script tag at the end of the body or wait for the DOMContentLoaded event.
Why does my JavaScript work on my computer but not on iPhone?
Common causes are a feature the browser engine on iPhone doesn't support yet, file names whose upper and lower case don't match, or code that relies on mouse events like hover instead of taps. The console usually shows which line fails.
Can I run JavaScript directly on iPhone?
Yes. Browsers on iPhone run the JavaScript in web pages, and in Code Editor you can also type JavaScript into the console prompt and see the result right away.