
Это второй день из 100 дней моего изучения javascript, и сегодня я продолжил узнавать больше о ключевом слове javascript var.
объявление и инициализация двух переменных:
var num1 = 100; var num2 = 200;
Это эквивалентно
var num1 = 100,
num2 = 200;
Когда переменные имеют одинаковое начальное значение, мы можем записать их как
var num1 = 100,
num2 = 100;
This is equivalent to
var num1,
num2 = num1 = 100;
Давайте сделаем некоторые упражнения
var a = 0,
b = 0;
console.log(a); //0
console.log(b); //0
var c,
d = 5;
console.log(c); //undefined
console.log(d); //5
// We get c as undefined because it is not be initialized
var c=d=5;
console.log(c); //5
console.log(d); //5
var e,
f = e = 10;
console.log(e); //10
console.log(f); //10
var g=h,
h = 20;
console.log(g); //undefined (be mindful of the order here)
console.log(h); //20
// Here g has been assigned value of h which is not yet initailized so has
// the value of undefined
Теперь давайте посмотрим на переменную, определенную таким образом в области видимости функции
function print() {
var a,b=15;
console.log(a); //15
console.log(b); //15
}
print();
console.log(a); //ReferenceError: a is not defined
console.log(b); // ReferenceError
// we get reference error for a and b since they have function scope,
// so not available outside print function
function print() {
var g= h =15;
console.log(g); //15
console.log(h); //15
}
print();
console.log(h); //15
console.log(g); // ReferenceError: g is not defined
// here we dont get error for h as var g= h =15 inside the function
// Declares x locally and declares y globally.
Это все на сегодня. Догонят завтра.
Спасибо за прочтение. Пожалуйста, хлопайте, если вам понравилась статья.
Продолжай учиться!
#100daysofjavascript