Javascript for Beginners!   Part-6πŸ”₯

Javascript for Beginners! Part-6πŸ”₯

Β·

4 min read

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:

image.png

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:

image.png

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 named x.

  • 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 as myColor() 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!!

Β