Heyy Coders!π©βπ»
Finally this is my Part-6 of Javascript for beginners!!π
And If you have not seen my Part-1, Part-2, Part-3, Part-4, Part-5 so you must see them and after that you have to see this one because in that 5 parts we have talked about much more!!π
So, Let's go and start learning!
Functions in JS!π
Basically, functions allow us to create reusable code in Javascript!
This is how a function is set up.π
Example:
function myFunction() {
console.log("Heyy World!!")
}
myFunction()
Output:
Explanation:
We used the function keyword and it's used in JavaScript to create a function!!
After I have called my function by writing
myFunction()
and also you can name it whatever you want!Then, I have putted some
curly braces
because without these we can't do this.- Now, in this curly braces I have done
console.log("Heyy World!!")
- After all these the myFunction() is being called!! Then see the Magicπ©.
Passing values in functions.π‘
Example:
function ourFunction(x, y) {
console.log(x - y)
}
ourFunction(100, 50)
Output:
Hopefully by this example you'll be having the idea of this!!β€
And if you are having any doubt regarding this please mention in the comments below!!π€
Global scopeπ and Functions.π
About Global scope: Basically global scope means it can be seen Globally anywhere.
Example for Global scope:
//Global Scope
var x = "Heyy!!!" // Global Scope
function myName() {
console.log(x) //"Heyy!!!"
}
console.log(x) //"Heyy!!!"
myName()
Output:
Heyy!!!
Heyy!!!
Let us understand the meaning of the above code:
First we have made a
var
namedx
.Then there is a function named
myName()
.Inside the curly braces :
console.log(x)
.After the curly braces again :
console.log(x)
.Then we have called our function
myName()
IN A SHORT WAY:
- You can see that x is a global variable that can be accessed inside myName() function. So myName() function will print the x variable, even though the x variable was not declared inside the function!!!π
Local Scope and Functions.π
Local scope basically can be returned in the function itself. EXAMPLE:
function myLocal() {
var z = "Cute" // Local Scope
console.log(z)
}
myLocal()
Output:
Cute
This is because we have made the var
inside the function and it will not come globally because it's the local function!!
Global and Local Scopes!!ππ
Example:
var x = "Heyy!!!" //Global Scope
function myName() {
var x = "Hellooo" //Local Scope
console.log(x) //"Hellooo"
}
myName();
console.log(x) //still the same (Heyy!!!)
Output:
Hellooo
Heyy!!!
Heyy smarties hopefully this example is easy to understand right??β₯
Let me give one more example:
var color = "Black" //Global scope
function myColor() {
return color
}
console.log(myColor())
Output:
Black
Explanation:
- Our Global scope is
"Black"
now function named asmyColor()
then we have returned the color so it'll be Black only. After that:console.log(myColor())
!! Easy Pizzy Lemon Squizy!!π
Now what if we include our local scope with the same variable name inside the function let's see!!!π€©
var color = "Black" //Global scope
function myColor() {
var color = "White"
return color
}
console.log(myColor())
Output:
White
This is because we have console myColor().
Now let's do with a twist!!π
var color = "Black"; //Global scope
function myColor() {
var color = "White"
return color
}
console.log(myColor())
console.log(color)
Output:
White
Black
Now after doing console.log(color)
we are getting Black
because it's a Global Scope as shown in the code above!!π
So, Guys I hope you all are totally done with the Global Scope and Local Scope!! If you are having any doubt please mention in the comments below.π
Motivation for Today: βYou can get everything in life you want if you will just help enough other people get what they want.β
So, this was my Part-6 for JavaScript for Beginners!! Now I will see you all again in the Part-7 blog!! Which will come tomorrow so stay tuned!!β₯ Till then Byee!!