Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 4, Topic 2
In Progress

JavaScript: Function Declarations

Yasin Cakal 5 Nov 2021
Lesson Progress
0% Complete

There are multiple ways to create a function in JavaScript. A function declaration is one way to create a function. A function declaration binds a function to a name, or an identifier, in the same way, that a variable declaration binds a value to a variable name. Take a look at the structure of a function declaration below:

function helloWorld() {
    console.log("Hello,World!");  
}

A function declaration consists of the following things:

  1. The function keyword is the first part of a function declaration.
  2. The function’s name or identifier, followed by parentheses ().
  3. The body of a function, or the block of statements required to complete a task, is enclosed in the function’s curly brackets{}.

A function declaration is a function that is associated with a name or identifier. We’ll go over how to run the code inside the function body in the next example.

We should also be aware of JavaScript’s hoisting feature, which allows us to access function declarations before they’ve been defined. Take a look at this hoisting example:

helloWorld();
function helloWorld() {
    console.log("Hello,World!");  
}

// Output: Hello,World!

It’s worth noting that hoisting allowed helloWorld() to be called before the helloWorld() function had been defined!

We simply want you to be aware of this feature because hoisting isn’t considered good practice. You can read  MDN documentation on hoisting to learn more about hoisting

Responses

Leave a Reply

%d bloggers like this: