101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    JavaScript 101

    Receive aemail containing the next unit.
    • Introduction to JavaScript
      • 1.1What is JavaScript?
      • 1.2Setting up your environment
      • 1.3Basic syntax and variables
    • Data Types and Variables
      • 2.1Understanding data types
      • 2.2Variables and constants
      • 2.3Operators
    • Control Structures
      • 3.1Conditional statements
      • 3.2Loops
      • 3.3Error handling
    • Functions
      • 4.1Defining functions
      • 4.2Function expressions
      • 4.3Arrow functions
    • Objects and Arrays
      • 5.1Understanding objects
      • 5.2Understanding arrays
      • 5.3Array methods
    • Document Object Model (DOM)
      • 6.1What is the DOM?
      • 6.2Selecting and manipulating elements
      • 6.3Event handling
    • Asynchronous JavaScript
      • 7.1Callbacks
      • 7.2Promises
      • 7.3Async/Await
    • API Interaction
      • 8.1What is an API?
      • 8.2Fetch API
      • 8.3Working with JSON
    • Debugging
      • 9.1Console methods
      • 9.2Debugging tools
      • 9.3Common JavaScript errors
    • JavaScript Libraries
      • 10.1Introduction to libraries
      • 10.2Using jQuery
      • 10.3Using lodash
    • JavaScript Frameworks
      • 11.1Introduction to frameworks
      • 11.2Using React
      • 11.3Using Vue.js
    • Testing and Deployment
      • 12.1Writing tests with Jest
      • 12.2Deployment with Netlify
      • 12.3Continuous integration
    • Final Project
      • 13.1Project proposal
      • 13.2Project development
      • 13.3Project presentation

    Functions

    Defining Functions in JavaScript

    high-level programming language

    High-level programming language.

    Functions are one of the fundamental building blocks in JavaScript. A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.

    Understanding What a Function Is

    In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want. A function can be defined using function keyword and can be invoked using function name followed by a list of arguments in parentheses.

    The Syntax of a Function

    A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). The code to be executed by the function is placed inside curly brackets {}. Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

    The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...). The code inside the function will execute when "something" invokes (calls) the function:

    function name(parameter1, parameter2, parameter3) { // code to be executed }

    Defining a Function Using the function Keyword

    Here's an example of how to define a function using the function keyword:

    function greet() { console.log('Hello, world!'); }

    In this example, greet is the function name, and the code inside the curly brackets {} is the function body. The function body is the code that will be executed each time the function is called.

    Function Parameters and Arguments

    Functions can take inputs, known as parameters. When you define a function, you can specify its parameters inside the parentheses (). Here's an example:

    function greet(name) { console.log('Hello, ' + name + '!'); }

    In this example, name is a parameter. When you call the function, you can provide an argument for this parameter:

    greet('Alice'); // Outputs: Hello, Alice!

    In this case, 'Alice' is the argument that is passed to the name parameter.

    The Return Statement

    A function can also produce an output, known as a return value. You can use the return keyword to specify the return value. Here's an example:

    function square(number) { return number * number; } console.log(square(4)); // Outputs: 16

    In this example, the square function takes one parameter, number, and returns the square of that number.

    In conclusion, understanding how to define and use functions is a key part of JavaScript programming. Functions allow you to write reusable code that can perform actions, calculate values, and more.

    Test me
    Practical exercise
    Further reading

    Hi, any questions for me?

    Sign in to chat
    Next up: Function expressions