Content of flow -:
Var (with code)
Let (with code)
Const (with code)
Quick Quiz
What next
var :
var keyword works just like government works. For example, the Government can access anything from anywhere & the ability change anything from anywhere. Right? Just like the var keyword works you can change the value from anywhere. That's why everyone called it the global scope. Global means You have a world. Let's understand with code. Note: It is a public variable, not a private one.
// 1. --------------------var----------------------
var myName = 'Roman'; // global variable : Imagine: you created a name in America
{
myName = 'Larry'; // trying to change in Delhi, good news you are able to change.
}
console.log(myName); // Output: Larry
// 2. ----------------------
{
var dateOfBirth = '20/08/2008'; // written your date of birth in Delhi
}
dateOfBirth = '20/09/2008'; // trying to change in America, Possible ? Ofcourse we are able to change.
console.log(dateOfBirth); // Output: 20/09/2008
let :
let keyword works just as private things work. Imagine you have your own BMW Car. now think about Can everyone operates the car & do customize it. Not, Right? Only its owner can do anything. Just like let works no one can't change from anywhere. That's why everyone is called a block scoped. Let's understand with code.
// 1. -----------------------let-------------------
let world = 'globalWorld'; // other person's world
{ // Private World
let world = 'privateWorld'; // another person's world
world = 'newPrivateWorld'; // changing on a run time
console.log(world); // Output: newPrivateWorld
}
console.log(world); // Output: globalWorld
// 2. --------------------let vs var -------------------
// -----var
var globalWorld = 'globalWorldOrGoverment';
{ // Global world
globalWorld = 'uniqueGlobalWorld'; // changing on a run time, Example: Just like everyone can do cutomize on a Highway road, park, but not private house
console.log(globalWorld); // Output: uniqueGlobalWorld
}
console.log(globalWorld); // Output: uniqueGlobalWorld
// -----let
let privateWorld = 'privateWorld'; // other person, like your house is private world
{ //another person private scope
privateWorld = 'otherPrivatteWorld' // another person world
console.log(privateWorld) // Output: otherPrivatteWorld
}
console.log(privateWorld) // Output: privateWorld
// Now you can compare it both var & let.
const :
imagine you wrote a book, then, of course, you are the author of that book, but if someone is telling you I am the author of this book, then what will you do with her? Won't you live here Right? Just like in the const keyword, you can't customize anything. I try you will get the error. or see this gif. Let's understand with code
// 1. -----------------const----------------------
const autorName = 'Roman';
// autorName = 'Larry'; // trying to change autor name
/*
Uncaught TypeError: invalid assignment to const 'authorName'
*/
console.log(autorName); // Output: Roman
Quiz Time:
Here I will write some quizzes you have to solve & DM me on my Twitter Link twitter.com/pycham_roman?t=sUHcgoFkPKPkwE8Y.. & I will announce your name & also follow you.
Quizzes:
Which keyword is used to declare a variable that cannot be reassigned?
a) const b) var c) let
Create a variable & print your name & change the name on a run time. using the var keyword.
Create a variable & print your favorite things & that will be able to change from anywhere. which keyword do you use to fulfill all the conditions (with code)?
Which keyword is allowed to customize from anywhere?
a) const b) var c) let
A person has a car his owner's name is Roman. someone wants to be the owner of that car, but Roman doesn't want it? In this case what keyword do you use to declare a variable? (with code & explanation) .
a) const b) var c) let
There's a palace, its name is 'Taj Mahal' someone wants to change its name, but its owner doesn't want. Is it possible? If yes or no (with code).
You have your house. Its name is Banglo. Which keyword is used to declare its name.?
a) const b) var c) let
Create a variable & pass the value on a run time (Dynamically).
What Next?
Tomorrow, we will learn about datatypes in Javascript & after that, we will be able to create our first javascript project.
On Thursday we will announce a project & in a few days we will be seeing who is a winner & also we will be creating that one project together with a better understanding. we will be using these concepts in our weekly projects
If you have any doubts or questions You can DM me on my Twitter I will try to fix that now enough & I will see you next time.