Back to Browse

40. Javascript Functions and methods - Full stack web development Course

19.5K views
May 27, 2018
22:20

In this FullStackWebDevelopment tutorial series video, We will learn about #javascript #functions and #return Statement. A JavaScript function is a block of code designed to perform a particular task. Function increases code reusability and decreases code length also makes it easier to debug. A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs. The parentheses may include parameter names separated by commas:(parameter1, parameter2, ...) The code to be executed, by the function, is placed inside curly brackets: {} Syntax: function name(parameter1, parameter2, parameter3) { // code to be executed } Example: function myFunction(a, a) {return a * b; // The function returns the product of p1 and p2} The code inside a function is executed when something calls or invokes a function i.e. when it is called from a javascript code, an event occurs(such as when a button is clicked) or sometimes self invoked. -- return Statement: var x = product(4, 3); // Function is called, function product(a, b) { return a * b; //returns the product of a and b, return value will end up in x} If a function is invoked from a statement, JavaScript will return to execute the code after the invoking statement. When JavaScript reaches a return statement, the function will stop executing. -- Function calling: With the call() method, we can write a method that can be used on different objects. It is a predefined JavaScript method used to invoke (call) a method with an owner object as an argument (parameter). This is helpful when an object wants to use a method belonging to another object. Example: var person = { fullName: function() { return this.firstName + " " + this.lastName; }} var person1 = { firstName:"Dane", lastName: "Price" } person.fullName.call(person1); // Will return "Dane Price" -- Function parameter: Function parameters are the names listed in the function definition whereas arguments are the real values passed to (and received by) the function. Syntax: function functionName(parameter1, parameter2, parameter3) { // code to be executed } JavaScript function definitions do not specify data types for parameters, do not perform type checking on the passed arguments and also do not check the number of arguments received. -- Function as object: In JavaScript, functions are objects. We can work with functions as if they were objects. For example, we can assign functions to variables, to array elements, and to other objects. They can also be passed around as arguments to other functions or be returned from those functions. JavaScript functions are a special type of objects, called function objects. A function object includes a string which holds the actual code -- the function body -- of the function. The code is literally just a string. Example: function Book (type, author) { this.type = type; this.author = author; this.getDetails = function () { return this.type + " written by " + this.author; }} var book = new Book("Fiction", "Peter King"); alert(book.getDetails()); // Fiction written by Peter King Here we are creating a new function with a custom constructor function. ---------------------------- Week 1 : Day 7 Section 5 : Learning to Code With Javascript Tutorial 40: Javascript Functions and return Statement ---------------------------- Do subscribe and hit Bell Icon ---------------------------- Follow us in social media handles for opportunities and code related support. Instagram: https://www.instagram.com/wb.web/ Facebook: https://www.facebook.com/wbweb/ Twitter: https://twitter.com/wbweb_in/ LinkedIn: https://www.linkedin.com/company/wbweb/ ---------------------------- Got a question on the topic? Please share it in the comment section below and our experts will answer it for you. For more information, please write back to us at [email protected] or call us at IND: 7077568998 After completing the course, write to [email protected] for internship or freelancing opportunities. For consultation or partnership related queries drop a mail to [email protected].

Download

0 formats

No download links available.

40. Javascript Functions and methods - Full stack web development Course | NatokHD