top of page
  • Writer's pictureMaster Hun

Hardcoding versus Variables

// 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>");






JavaScript code edited in Scratchpad (Firefox).
Master Hun's JavaScript code.

57 views0 comments

Recent Posts

See All
bottom of page