Returning Values from Functions
Often, a function needs to return a value to the calling code. Adding a return
keyword followed by a variable or value returns that value from the function. For example, the following code calls a function to format a string, assigns the value returned from the function to a variable, and then writes the value to the console:
function formatGreeting(name, city){ var retStr = ""; retStr += "Hello <b>" + name + "/n"); retStr += "Welcome to " + city + "!";return retStr;}var greeting = formatGreeting("Brad", "Rome");console.log(greeting);
You can include more than one return
statement in the function. When the function encounters a return
statement, code execution of the function stops ...
Get Learning AngularJS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.