Loading
Current section: 5. Functions 7 exercises
solution

Function Declaration

Transcript

00:00 Okay. Let's make this function declaration. So we're gonna make function called get message and return our value. And yep, AI knows exactly what to do here. So what is this?

00:12 What are we looking at? This is called a function declaration. Starts with the keyword function. You give it a space to separate function from the name of the function. And then you give these parenthesis.

00:22 We'll talk about what those parenthesis can be used for later, what can go in between them. And then we have this return value that we're specifying. So it's a colon and then the the type of value that we are returning. And then, we have our block. This is what we actually call a closure in JavaScript.

00:39 So variables defined in here, will exist as long as, this function exists. And it's a pretty interesting, thing to consider with garbage collection, all this stuff that we're gonna get into later. But inside of this closure, we can do whatever we want to. But at the very end, we're gonna have a return statement, that here we're saying hello functions and that has to match our return value. So if I save this, we're gonna get our log because of this log and then we'll see our export, Just a function called get message.

01:09 And, if we were to try to return number here, we're gonna get a type error because that doesn't match our return value. If I change this to number, then now we're gonna get that return value, in return. And, yeah. That is, the very basics of our function. We've we've got lots more to talk about with functions.

01:30 They're really powerful, primitive of, JavaScript or or really any language being able to reuse this stuff and and everything. So, this is very important part of learning how to develop software. And this is that that piece. The cool thing about this too is we can, do this as many times as we want to and now we get hello functions. And then we can say, you know what?

01:53 I wanna be really excited about this. And so we just change it there once and we get it all, applied everywhere else that we're calling the function. That might look a little bit like a loop and, yeah, you could definitely use this in a loop. But the the point is that you don't have to use it in a loop. You can, call it the function from wherever, that function is accessible.

02:14 And I think that's pretty cool. So there you go. That is your first introduction to functions. Hello, functions.