1. With ES6 classes Read-only “power” property example:- For power property, let’s make it read-only, It sometimes happens that a property must be set at creation time only, and then never modified.That’s exactly the case for a coffee machine, power never changes. To do so, we only need to make getter, but not the setter:

class CoffeeMachine {
  constructor(power) {
    this._power = power;
  }

  get power() {
    return this._power;
  }

}

// create the coffee machine
let coffeeMachine = new CoffeeMachine(100);

alert(`Power is: ${coffeeMachine.power}W`); // Power is: 100W

coffeeMachine.power = 25; // Error (no setter)

2. With ES5

//In below marks is private
function Employee() {
  var marks=40;
  return {
    getMarks:function() {
	  return marks;
	},
	setMarks:function(value) {
	  marks=value;
	}
  }
}
var test=Employee();
console.log(test.getMarks()); //40
test.setMarks(70);
console.log(test.getMarks()); //70
console.log(test.marks) // undefined

In above code 'marks' variable is private variable, it can be accessed using getMarks() method and can be set Using setMarks() method, but can't be accessed directly.