Note 6 : Functions

  • It is a fundamental building block of JavaScript. it is basically a set of statements to perform some tasks and calculate the value.
  • while declaring any function we don't need to add semicolumn at the end like we have declared any variables before.

  • but the functions are more than rather only declaring and calling we can make this more interesting by passing arguments and call that function.

  • Here one thing we have to notice that when we pass in anything in the function call is known as an argument. like here in the above example printMessage('This is the message') the sting here in 'This is the message' is an argument. and a message is written in the function printMessage(message) is a parameter here in printMessage function.
  • the main fundamental things here to work like this is we can reuse the printMessage function.
  • a function may have multiple parameters.

  • the default value of the parameter is undefined.

Types of function :

  • or we can write this like this.

  • the function is a set of statements either perform some task or calculate some values.
  • there are two ways to declare any function. function declaration and function expression. here in javascript, we know that functions are also an object.

  • here above image is clearly understandable. but for explanation can say that there is a difference between anonymous function and function declaration... 
  • we have to complete the anonymous function with a semicolumn. and we don't have to follow the same to the function declaration.
  • here anonymous function is referencing the run variable, so we can call that function with the same.
we can pass the same to another variable like here we have pass run to fast-Run so now in memory there both are referencing to the same function.

here in the above example, a key difference is we can call function declared function printMessage() before it was defined. but we cannot do this with the function expression syntax.



  • so why this different .? it happens because when javascript executes this code it moves all the function declaration to the top and this process  Is known as hoisting. so, hoisting is the process of moving function declaration to the top of the file. and this is done automatically by the javascript who is executing this code.
  • earlier we see that in JavaScript variables are dynamic so whatever value we have given to the variable we can change after that. the same principle applies to the function argument.
  • if we don't give a second argument that is required it will automatically take that as an undefined. example as below.


  • same rules apply if we are passing more parameters then require. the function will take only the number of argument which is defined.
  • but what if we want to give flexibility for the argument.
  • we can do this in a better way. in modern javascript, if we have a various number of argument we can use the rest operators.

  • so in modern javaScript, we can achieve the same result with less code.
  • but also we have to notice that the rest operator should be at last in the function parameter.
  • DEFAULT PARAMETER: there are times when we want to supply default values to the parameters of a function.

  • starting from the ES6 we have a much cleaner way to achieve the same thing.

  • so if we don't give value to other parameter the function will take value from default parameters. here, one thing we have to notice that if we are giving default value to the parameter we have to give value to the other parameter also after that.

  • the above stuff is valid but it looks very ugly that is why the best practice is to give other parameter default value too.

GETTER - SETTER :



TRY- CATCH :
























Comments

Popular posts from this blog

NOTE 1: JavaScript introduction

Note 5: Arrays.