Assignment Operators


Assignment Operators

The equal (=) assignment operator assigns the value of the right-hand operand to the left-hand operand. If a = b sets the value of b to a, this is the case.

To assign a value to a variable, use the simple assignment operator. The assigned value is the result of the assignment process. The assignment operator can be chained to assign a single value to many variables.

Syntax:

data=value

Examples:

Examples:
// Lets take some variables
x=20
y=10

x=y // Here, x is equal to 10
y=x // Here, y is equal to 20

add properties to a constructor

function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

const member = new Person('Harshita', ‘Shrivastava');
Person.getFullName = function() {
return `${this.firstName} ${this.lastName}`;
};

console.log(member.getFullName());

Like ordinary objects, you can't add attributes to constructors. You must use the prototype instead if you wish to add a feature to all objects at once. So, in this instance,

Person.prototype.getFullName = function() {
return `${this.firstName} ${this.lastName}`;
};

Would have been accepted as a member getFullName() is useful. What are the advantages of this? Let's pretend we introduced this function to the function Object() { [native code] }. This technique was probably not required in every Person instance. This would waste a lot of memory because they'd still have that property, which consumes memory for each instance. Instead, if we just add it to the prototype, we'll just have it in one place in memory, but it'll be accessible to everyone!

Example No.1:

<html> <body> <h2>Knowledge2life</h2> <p id="demo"></p> <script> let x = 10; x += 5; document.getElementById("demo").innerHTML = x; </script> </body> </html>

OUTPUT:

Knowledge2life

15