Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Assignment in JavaScript

Js Assignment in JavaScript

? JavaScript Assignment


What is Assignment in JavaScript?

  • Assignment means giving a value to a variable.

  • Done using the assignment operator =.


Basic Variable Assignment

let x = 5;       // Assign 5 to variable xconst name = "Alice";  // Assign string to constant namevar isActive = true;   // Assign boolean to variable isActive

Multiple Assignments

let a, b, c;a = b = c = 10;   // Assign 10 to c, then b, then aconsole.log(a, b, c);  // 10 10 10

Destructuring Assignment (ES6)

Array Destructuring

const arr = [1, 2, 3];const [first, second] = arr;console.log(first, second);  // 1 2

Object Destructuring

const obj = { name: "Bob", age: 25 };const { name, age } = obj;console.log(name, age);  // Bob 25

Compound Assignment Operators

Short-hand to assign with an operation:

OperatorEquivalent ToExample
+=x = x + yx += 5
-=x = x - yx -= 3
*=x = x * yx *= 2
/=x = x / yx /= 4
%=x = x % yx %= 3
let num = 10;num += 5;    // num is now 15

Assignment with Functions

let sayHello = function() {  console.log("Hello!");};// Or using arrow functionlet greet = () => console.log("Hi!");

If you want, I can explain how assignment works with objects, arrays, or difference between var, let, and const!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql