Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

JavaScript - Variable

Back


Variable


Declaration

// Using var
var variable_name;

// Using let
let variable_name;

// Using const
const constant_name;

// declare many variables in one statement.
let person = "John Doe", carName = "Volvo", price = 200;

// A declaration can span multiple lines:
let person = "John Doe",
carName = "Volvo",
price = 200;

<p id="demo"></p>

<script>
  var carName = "Volvo";
  var carName;
  document.getElementById("demo").innerHTML = carName; //show Volvo
</script>

Identifiers

let $ = "Hello World";
let $$$ = 2;
let $myMoney = 5;

let

let x = "John Doe";

let x = 0; // SyntaxError: 'x' has already been declared

var x = "John Doe";

var x = 0; // Redeclared
{
  let x = 2;
}
// x can NOT be used here

{
  var x = 2;
}
// x CAN be used here

Redeclaring Variables

var x = 10;
// Here x is 10

{
  var x = 2; //redeclare using var
  // Here x is 2
}

// Here x is 2. The value will be changed in the block

// ----------------------------
let x = 10;
// Here x is 10

{
  let x = 2; //redeclare using let
  // Here x is 2
}

// Here x is 10. The value outside the block will not be affected by the block.

Hoisting

<p id="demo"></p>

<script>
  try {
    carName = "Saab";
    let carName = "Volvo";
  } catch (err) {
    document.getElementById("demo").innerHTML = err; //ReferenceError: Cannot access 'carName' before initialization
  }
</script>

const

<p id="demo"></p>

<script>
  // Create an Array:
  const cars = ["Saab", "Volvo", "BMW"];

  // Change an element:
  cars[0] = "Toyota";

  // Add an element:
  cars.push("Audi");

  // Display the Array:
  document.getElementById("demo").innerHTML = cars;

  // cars = ["Toyota", "Volvo", "Audi"];    // ERROR: redeclare
</script>
<p id="demo"></p>

<script>
  // Create an object:
  const car = { type: "Fiat", model: "500", color: "white" };

  // Change a property:
  car.color = "red";

  // Add a property:
  car.owner = "Johnson";

  // Display the property:
  document.getElementById("demo").innerHTML = "Car owner is " + car.owner;

  // car = {type:"Volvo", model:"EX60", color:"red"};    // ERROR: redeclare
</script>

Redeclaring

var x = 2; // Allowed
const x = 2; // Not allowed

{
  let x = 2; // Allowed
  const x = 2; // Not allowed
}

{
  const x = 2; // Allowed
  const x = 2; // Not allowed
}

Block Scope

const x = 10;
// Here x is 10

{
  const x = 2;
  // Here x is 2
}

// Here x is 10

Hoisting

<p id="demo"></p>

<script>
  try {
    alert(carName);
    const carName = "Volvo";
  } catch (err) {
    document.getElementById("demo").innerHTML = err; //ReferenceError: Cannot access 'carName' before initialization
  }
</script>

Destructuring assignment

Unpack Array

// destructuring array
const arr = [1, 2, 3, 4];
const [a1, b1] = arr;
// console.log(a1); //1
// console.log(b1); //2

const [a2, b2, c2, d2, e2] = arr;
console.log(a2); //1
console.log(b2); //2
console.log(c2); //3
console.log(d2); //4
console.log(e2); //undefined

const [a3, b3, ...c3] = arr;
console.log(a3); //1
console.log(b3); //2
console.log(c3); //[ 3, 4 ]

Unpack Object

const obj01 = { b1: 2, a1: 1, c1: 3, d1: 4 };
const { a1, b1 } = obj01; //相当于 a=obj.a
// console.log(a1); //1
// console.log(b1); //2

const obj02 = { b3: 2, a2: 1, c2: 3, d2: 4 };
const { a2, b2, c2, d2, e2 } = obj02;
console.log(a2); //1
console.log(b2); //2
console.log(c2); //3
console.log(d2); //4
console.log(e2); //undefined, 因为 obj 没有 e2 的属性

const obj03 = { b3: 2, a3: 1, c3: 3, d3: 4 };
const { a3, b3, ...c3 } = obj03;
console.log(a3); //1
console.log(b3); //2
console.log(c3); //{ c3: 3, d3: 4 }

const obj04 = { b4: 2, a4: 1, c4: 3, d4: 4 };
const { a4: x, b4: y } = obj04; //相当于 x=obj04.a4
console.log(x); //1
console.log(y); //2

TOP