Hardcoding versus Variables
- Master Hun
- Feb 9, 2019
- 1 min read
// The following is JavaScript code.
//
// Why using variables is better than
// hardcoding.
//
// Which is easier, smarter?
// Which is harder to maintain,
// to change?
// Example of hardcoding
// If you want to change "Joe Smith" to a
// to a different name.
// You will have to change "Joe Smith" nine times.
document.writeln("Joe Smith was hardcoded.");
document.writeln("</br>");
document.writeln("Joe Smith is sad.");
document.writeln("</br>");
document.writeln("Joe Smith is bad.");
document.writeln("</br>");
document.writeln("Joe Smith is mad.");
document.writeln("</br>");
document.writeln("Joe Smith is big.");
document.writeln("</br>");
document.writeln("Joe Smith is smelly.");
document.writeln("</br>");
document.writeln("Joe Smith is silly.");
document.writeln("</br>");
document.writeln("Joe Smith is hungry.");
document.writeln("</br>");
document.writeln("Joe Smith is funny.");
document.writeln("</br>");
document.writeln("</br>");
document.writeln("</br>");
document.writeln("</br>");
document.writeln("</br>");
// Or?
// If instead you used a variable.
// You only have to change the
// variable userName ONCE!
var userName = "Joe Smith";
document.writeln(userName + " is the string in the variable userName.");
document.writeln("</br>");
document.writeln(userName + " is sad.");
document.writeln("</br>");
document.writeln(userName + " is bad.");
document.writeln("</br>");
document.writeln(userName + " is mad.");
document.writeln("</br>");
document.writeln(userName + " is big.");
document.writeln("</br>");
document.writeln(userName + " is smelly.");
document.writeln("</br>");
document.writeln(userName + " is silly.");
document.writeln("</br>");
document.writeln(userName + " is hungry.");
document.writeln("</br>");
document.writeln(userName + " is funny.");
document.writeln("</br>");
document.writeln("</br>");
document.writeln("</br>");
document.writeln("</br>");
document.writeln("</br>");
// The variable userName is now a
// different name.
// The variable userName was "Joe Smith".
// Now it's "Lee Young".
// You only have to change one variable
// ONCE. Instead of nine or more times
// Hardcoding vs. variables.
// More work vs. less work.
// userName was "Joe Smith".
userName = "Lee Young";
// Now userName is "Lee Young".
document.writeln(userName + " is the string in the variable userName.");
document.writeln("</br>");
document.writeln(userName + " is sad.");
document.writeln("</br>");
document.writeln(userName + " is bad.");
document.writeln("</br>");
document.writeln(userName + " is mad.");
document.writeln("</br>");
document.writeln(userName + " is big.");
document.writeln("</br>");
document.writeln(userName + " is smelly.");
document.writeln("</br>");
document.writeln(userName + " is silly.");
document.writeln("</br>");
document.writeln(userName + " is hungry.");
document.writeln("</br>");
document.writeln(userName + " is funny.");
document.writeln("</br>");

Curious about how old you really are—down to the exact day, hour, or even second? Our powerful and easy-to-use Age calculator takes the guesswork out of aging by instantly providing your precise age based on your date of birth. Whether you're planning a special celebration, tracking milestones, or just having fun comparing ages with friends, this tool gives you detailed results in years, months, days, and more. There's no need to sign up or install anything—just enter your birthdate into our online age calculator and get instant, accurate results right from your phone, tablet, or computer. It's completely free, secure, and works smoothly on any device. Perfect for birthdays, anniversaries, fun facts, or even just to satisfy your curiosity—give it a try…