Javascript Naming Convention: Value() vs getValue() [Closed]

Imagine a simple object:

function createObj(someValue) {
    return {
        someFunction: function () {
            return someValue;
        }
    };
};

It basically does:

var obj = createObj("something");
var result = obj.someFunction(); // "something"

Now, someFunction refers to a function that returns a value. What should be the correct naming convention used in javascript for the someFunction function? Should it be named as what it does? Or its ok to name it with the name of the object that it returns?

I mean, should I name it value(), or should I name it getValue()? Why?

Thanks in advance.

4

4 Answers

There's no "correct" naming in JavaScript. However, there are three conventions that are mostly used:

Independent getter and setter:

In this approach, we create both a getter and a setter functions, like in Java:

var value;

this.getValue = function () {
    return value;
}

this.setValue(val) {
    value = val;
}
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.