Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I've been learning java for about a year, but recently entered the world of javascript. I'm having trouble coming up with meaningful names in function parameters, so that I'll be able to tell and remember what is being passed in and act on it correctly. For example:

In java, I could define void method(Type name) and always know that name is of object Type.

void printToConsole(String errorMessage) {
    System.out.println(errorMessage); <--I know errorMessage is a string

In Javascript, I can only do function method(name), and if I come back later, I forget what name is and have to go see where method was called and what was passed in. I have started naming the parameter like nameType but this seems too verbose.

somewhere in code:


function getBoxText(searchBox) {
    //TODO: do something with searchBox;

In the second example, its not immediately clear that searchBox is an element, as opposed to a nodeList, XML DOM, or something else, so when I come back to write this method, I have to go find the call and remember what it is. I would be tempted to change the function declaration to:

function getBoxText(seachBoxElement) {

Even worse, later I might be tempted to call getBoxText(somethingNotAnElement).

Is there an accepted convention for this? Thoughts?

share|improve this question

closed as primarily opinion-based by durron597, MichaelT, GlenH7, Snowman, enderland Aug 13 at 23:54

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

for the "Even worse, later I might be tempted to call getBoxText(somethingNotAnElement)" part you could use good documentation, assertion and test to prevent or to contain the damage. I don't remember where I read it but I read that weakly type can become almost safe as strong type with good unit testing. For the naming part I guess you should search for JS variable guidelines and best practice. –  Fabio Marcolini Feb 5 '14 at 20:30
Another thing you could do is to validate your javascript using something like the closure compiler from google. As you can see it permit do define some parameter constraint that are checked by the compiler. –  Fabio Marcolini Feb 5 '14 at 20:44
There is always: function getBoxText(searchBox /*element*/) { –  psr Feb 5 '14 at 21:48
Somewhat of a nitpick, but the language aspect you are trying to deal with here is dynamic typing, not weak typing. JS can indeed behave like weakly typed language, but your question here refers to the fact that the same variable can hold objects of completely unrelated types. This is strictly a trait of dynamically typed languages which include JavaScript, Python, Ruby, and others. –  Xion Feb 5 '14 at 22:43
searchBox sounds like a perfectly good name that makes it blindingly obvious that you're meant to pass an element (specifically, a textbox) to it. Unless you're in an environment where there are multiple ways to refer to such a thing (e.g. a raw DOM element or a jQuery element wrapper), in which case additional clarification may be needed (rawSearchBox for the DOM element, for example). –  Sebastian Redl Feb 6 '14 at 11:06

4 Answers 4

There are a few different ways people handle it:

  • Search for an existing call (what you're already doing).
  • Infer the type from the body of the function. For example, in your printToConsole function, you can infer from the body that errorMessage is either a String or an object with a reasonable toString method.
  • Name your function better. For example, getBoxElementText.
  • Document the type with a comment, often a javadoc-style comment.
  • Check the types at the top of the function, if possible.
share|improve this answer
+1 All good points. Too bad most JS programmers apparently didn't learn how to use // and /* –  user949300 Feb 5 '14 at 22:02
@Karl Bielefeldt: The link to Jimmy Hoffa's answer is broken. I'd be interested to read it if you can find the correct link? –  AndyBursh Feb 6 '14 at 8:56
He deleted it, sorry. He provided an interesting point, but it wasn't crucial. –  Karl Bielefeldt Feb 6 '14 at 13:06

Languages that are absurdly weakly typed are literally what Hungarian Notation was devised to cope with. Instead of relying upon a language feature to protect you from code bugs, you can rely upon the name of the variable to impart some type information.

Of course, Hungarian Notation can be taken way too far. Joel Spolsky wrote Making Code Look Wrong, which is a very good explanation to how HS can be used properly (If you want an explanation for how HS can be used wrongly, there are ample sources available online.)

In general, my experience is that lagnuages without type-checking as a feature force you to spend a modest amount of time checking for invalid parameters sent in to our public entry points -- and in JavaScript, that's almost every function. If you're concered about it, a simple set of re-usable functions may be a good time investment.

share|improve this answer

I'm in the same boat, playing with JS the fast few months after a long bout with Java.

I don't have a great answer, and am hoping for some good responses, but two things help me.

  1. If the var is a string and is the name of something, say, a book, call it bookName (or bookTitle might be better here) NOT a book. Avoid the simple "book", which might be the name of a book, or some object representing a book.

  2. Be anal about plurals. If a variable ends with "s", it should (almost always) be an array or possibly a hash - a basic {} type "object" with key-value pairs. For example, the signature to find a book in a nearby library might be

findBook(bookTitle, libraries, options)

where libraries is an array of acceptable (nearby) libraries, and options would have key-value pairs like

    mustBeInStock : true, 
    languages: ['English','Spanish'] 
share|improve this answer

I think that you want to use JSDoc system. It is commonly used in other dynamic programming languages, and even in some static typed if you want to better explain what some variables hold / methods do, or what is responsibility of objects.

This is just convention, but there are lots of other tools built around that convention so you can use it to generate documentation, when using IDE or advanced editor, it can give you hints to what types function expects instead of only telling you variable names.


 * Creates an instance of Circle.
 * @constructor
 * @this {Circle}
 * @param {number} r The desired radius of the circle.
function Circle(r) {
    /** @private */ this.radius = r;
    /** @private */ this.circumference = 2 * Math.PI * r;
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.