How to make variables private in Constructor functions in JavaScript ? Last Updated : 10 Feb, 2022 Comments Improve Suggest changes Like Article Like Report In Javascript there's no specific way to make variables private in a Constructor function. If a variable is genuinely private, meaning it cannot be accessible from the outside. A constructor is a function that generates a new instance of a class, sometimes referred to as an "object." A constructor's job is to build an object and assign or set values for the properties of the object if they're present. Let's see how to make variables private in a constructor function in this article by looking at a few examples. Example 1: There are two methods defined on the prototype, getElement, and setElement. The setElement doesn't work. In the below example we create an object a with value 15, the same value is given to variable b to show that we can retrieve the value using getElement method but we cannot set or override the value by using setElement method, as args isn't declared as a variable, it's passed on to a new variable, so it stays private. By declaring the variables in the below manner, the variable cannot be further modified by any of its methods, it stays private. JavaScript <script> function func(args) { var element = args; this.getElement = function () { return element; }; this.setElement = function (input) { element = input; }; } var a = new func(15); var b = a.getElement(); console.log(b); a.setElement(5); var c = a.element; console.log(c); </script> Output: 15 undefinedExample 2: In the before method, a variable is declared and the value passed into the function is given to it. No new variable is declared to pass on the value of args in this example. So we can not change the value of a variable that's not defined in the function. So setElement doesn't help us create a new variable just like the previous example. As there is the perfect way to make variables private in a javascript constructor these methods can be used. It allows our values to not be changed in a javascript constructor. JavaScript <script> function func(args) { this.getElement = function () { return args; }; this.setElement = function (input) { args = input; }; } var a = new func(10); var b = a.getElement(); console.log(b); a.setElement(5); var c = a.element; console.log(c); </script> Output: 10 undefined Comment More infoAdvertise with us Next Article How to make variables private in Constructor functions in JavaScript ? S sarahjane3102 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to create a private variable in JavaScript ? In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari 3 min read How Inheritance works in Constructor Functions in JavaScript ? Here, we will discuss inheriting a constructor function in JavaScript. Constructor functions define the prototype of the properties an object will contain. Using the constructor function, we can create a new object after passing the required parameters. Inheriting a previously defined constructor fu 3 min read What is the (function() { } )() construct in JavaScript? If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit 3 min read How to create Static Variables in JavaScript ? To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth 2 min read How to transform String into a function in JavaScript ? In this article, we will see how to transform a String into a function in JavaScript. There are two ways to transform a String into a function in JavaScript. The first and easy one is eval() but it is not a secure method as it can run inside your application without permission hence more prone to th 3 min read JavaScript Function() Constructor The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne 2 min read How to use a closure to create a private counter in JavaScript ? A closure is a combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer functionâs scope from an inner function. Example: Here the inner forms closure with outer. The str variable 4 min read JavaScript Course Variables in JavaScript Variables in JavaScript are containers that hold reusable data. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory loca 4 min read How to create a variable using a user-defined name in JavaScript ? In this article, we will learn to create a variable using the user-defined name in JavaScript. Variables in JavaScript: Basically, it is a container that holds reusable data in JavaScript. The variable's value can be changed during program execution and should be declared before using it. Before ES6 2 min read How to declare Global Variables in JavaScript ? Global Variables Global variables in JavaScript are variables declared outside of any function. These variables are accessible from anywhere within the script, including inside functions. Global variables are declared at the start of the block(top of the program)Var keyword is used to declare variab 2 min read Like