Variables added to the global WINDOW object CODE
Variable
lastName
declared by the keyword
let
is not added to the global
window
object. That is why the variable is equal to
undefined
// Declaring variable using keyword "var"
var firstName = "Jet"

// Declaring variable using keyword "let"
let lastName = "Li"

// Declaring variable without using any keyword
carMark = "Tesla"

function driveCar() {
  console.log(this.firstName + ' ' + this.lastName + ' is driving a car '+this.carMark)
}

driveCar()
The result will be:
Jet undefined is driving a car Tesla
Because declaring variable with
var
keyword and without any declaration keyword are the same. Both add the variable to the global
window
object. After running the above code, global object
window
will have
firstName
and
lastName
attributes
window.firstName;  // "Jet"
window.lastName;  // undefined
window.carMark;  // "Tesla"
by Valeri Tandilashvili
4 years ago
JavaScript
THIS
2
Pro tip: use ```triple backticks around text``` to write in code fences