đź§ JavaScript, Integer Limits, and a Missing SMS: A Debugging Story
đź‘‹ Introduction
As a backend developer stepping into the world of JavaScript, I recently had a debugging experience that was both frustrating and enlightening. This blog is about how a simple data type mistake led to an undelivered SMS - and how I discovered one of JavaScript’s quirks in the process.
📦 The Setup
I was building a JavaScript program to send SMS notifications using a third-party SMS provider. Here’s how I structured it:
- Configuration File: Included SMS provider credentials and the template ID used for sending messages.
- Template ID: Each message to be sent needs a pre-approved template ID registered with the SMS provider.
- Functionality: Read the credentials and template ID from config, call the SMS API, and log the response.
🕵️ The Problem
The next day, I decided to run a test again. This time:
- The SMS was sent to the provider (request was successful).
- But the SMS was not delivered to the phone.
❌ "Template not registered." But I had already registered the template. And it was working the previous day!
đź§© The Mystery
I double-checked everything:
- ✅ Template ID in configuration → Correct
- ✅ Registered in provider’s dashboard → Correct
- ✅ Code not changed since yesterday → Correct
🔍 The Root Cause
I started debugging the actual request being sent.
Here’s what I found:
- Yesterday's working value was:
templateId: "1507100649629881791" // String - Today's failing value was:
templateId: 1507100649629881791 // Integer
In JavaScript, the maximum safe integer is:
Number.MAX_SAFE_INTEGER // 9007199254740991
If you go beyond that, JavaScript starts approximating, and the number gets silently altered. So, when I passed the template ID as an integer, JavaScript changed the actual value - leading to an invalid ID being sent to the provider.
đź’ˇ The Lesson
This was a major “Aha!” moment for me.
Always treat large numeric IDs (like template IDs, phone numbers, etc.) as strings, not numbers.
Why?
- JavaScript cannot safely handle integers beyond a certain size.
- APIs often expect exact values.
- String representation keeps the value intact and precise.
âś… Final Fix
I updated the config to wrap the template ID in quotes, like this:
"templateId": "1507100649629881791" // String
Re-tested, and voilà ! SMS delivered 🎉
🪄 Takeaway for New JavaScript Developers
If you're from a strongly typed language background like C# or Java (like I am), JavaScript’s handling of numbers can surprise you.
Here are some quick tips:
- Use typeof to verify the actual type at runtime.
- Treat any large numeric ID (like UIDs, phone numbers, etc.) as strings.
- Know the limits:
Number.MAX_SAFE_INTEGER === 9007199254740991
đź‘‹ Final Thoughts
This may seem like a small issue, but it reminded me of how data types matter - especially in loosely typed languages. I’m still early in my JavaScript journey, but this experience taught me to respect the quirks of the language.
I hope sharing this helps someone else avoid a few hours of head-scratching!