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
    |
    4 Quizzes
  9. JavaScript: Iterators
    1Topic
    |
    1 Quiz
Lesson 4, Topic 1
In Progress

JavaScript: Introduction to Functions

Yasin Cakal 5 Nov 2021
Lesson Progress
0% Complete

When learning how to calculate the perimeter of a square, there are a few steps to follow in order to get the right answer:

  1. Take measurement of the square’s length.
  2. Multiply the length with 4.

With practice, you’ll be able to calculate the perimeter of the square without needing to be guided through these two steps each time. The following code can be used to calculate the perimeter of a square:

const length= 10;
const perimeter=  length*4;
console.log(perimeter);

//40

Consider the following scenario: You’ve been asked to calculate the perimeter of three different squares:

//Perimeter of first square
var length= 12;
var perimeter=  length*4;
console.log(perimeter);

//Perimeter of second square
var length= 18;
var perimeter=  length*4;
console.log(perimeter);

//Perimeter of third square
var length= 19;
var perimeter=  length*4;
console.log(perimeter);


//Output: 48 72  76

In programming, we frequently use code to perform a task multiple times. Rather than rewriting the same code, we can group a block of code and associate it with a single task, then reuse that block of code whenever the task is repeated. This is accomplished through the use of a function. A function is a reusable code block that combines a series of statements to accomplish a specific task.

This lesson will teach you how to create functions and use them to write clearer and more concise code.

Responses

Leave a Reply

%d bloggers like this: