JS Assignment

JS Assignment

JS Assignment

 

In JavaScript, assignment operators are used to assign values to variables. They are a shorthand way of writing a statement that would otherwise be written as two separate statements: one to declare the variable and another to assign a value to it. The basic assignment operators are:

  1. Assignment (=): Assigns a value to a variable.

Example:

				
					let x;

x = 5; // Assigns the value 5 tothe variable x

console.log(x);// Output: 5
				
			
  1. Addition assignment (+=): Adds a value to a variable and assigns the result to the variable.

Example:

				
					let x = 5;

x += 3; // Adds 3 to x andassigns the result to x

console.log(x); // Output: 8
				
			
  1. Subtraction assignment (-=): Subtracts a value from a variable and assigns the result to the variable.

Example:

				
					let x = 5;

x -= 3; // Subtracts 3 from xand assigns the result to x

console.log(x); // Output: 2
				
			
  1. Multiplication assignment (*=): Multiplies a variable by a value and assigns the result to the variable.

Example:

				
					let x = 5;

x *= 3; // Multiplies x by 3 andassigns the result to x

console.log(x); // Output: 15
				
			
  1. Division assignment (/=): Divides a variable by a value and assigns the result to the variable.

Example:

				
					let x = 5;

x /= 3; // Divides x by 3 andassigns the result to x

console.log(x); // Output:1.6666666666666667
				
			
  1. Modulo assignment (%=): Divides a variable by a value and assigns the remainder to the variable.

Example:

				
					let x = 5;

x %= 3; // Divides x by 3 andassigns the remainder to x

console.log(x); // Output: 2
				
			

These assignment operators can be used to simplify your code and make it more readable by combining the declaration and assignment of variables into a single statement.

Join To Get Our Newsletter
Spread the love