Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary javascript functions

Rating
-
Sold
-
Pages
20
Uploaded on
01-05-2023
Written in
2022/2023

It's an note about javascript function.It's very simple and easy to understand by the beginners

Institution
Course

Content preview

FUNCTIONS
People think that computer science is the art of geniuses but the actual reality is
the opposite, just many people doing things that build on each other, like a wall of
mini stones.

Donald Knuth




Functions are the bread and butter of JavaScript programming. The
concept of wrapping a piece of program in a value has many uses. It
gives us a way to structure larger programs, to reduce repetition, to
associate names with subprograms, and to isolate these subprograms
from each other.

The most obvious application of functions is defining new vocabulary.
Creating new words in prose is usually bad style. But in programming,
it is indispensable.

Typical adult English speakers have some 20,000 words in their
vocabulary. Few programming languages come with 20,000 commands
built in. And the vocabulary that is available tends to be more precisely
defined, and thus less flexible, than in human language. Therefore, we
usually have to introduce new concepts to avoid repeating ourselves too
much.

,Defining a function
A function definition is a regular binding where the value of the binding
is a function. For example, this code defines square to refer to a
function that produces the square of a given number:
edit & run code by clicking it
const square = function(x) {
return x * x;
};

console.log(square(12));
// → 144

A function is created with an expression that starts with the
keyword function. Functions have a set of parameters (in this case,
only x) and a body, which contains the statements that are to be
executed when the function is called. The function body of a function
created this way must always be wrapped in braces, even when it
consists of only a single statement.

A function can have multiple parameters or no parameters at all. In the
following example, makeNoise does not list any parameter names,
whereas power lists two:
const makeNoise = function() {
console.log("Pling!");
};

makeNoise();
// → Pling!

const power = function(base, exponent) {
let result = 1;
for (let count = 0; count < exponent; count++) {
result *= base;
}
return result;
};

console.log(power(2, 10));
// → 1024

, Some functions produce a value, such as power and square, and some
don’t, such as makeNoise, whose only result is a side effect.
A return statement determines the value the function returns. When
control comes across such a statement, it immediately jumps out of the
current function and gives the returned value to the code that called the
function. A return keyword without an expression after it will cause
the function to return undefined. Functions that don’t have
a return statement at all, such as makeNoise, similarly
return undefined.

Parameters to a function behave like regular bindings, but their initial
values are given by the caller of the function, not the code in the
function itself.

Bindings and scopes
Each binding has a scope, which is the part of the program in which the
binding is visible. For bindings defined outside of any function or
block, the scope is the whole program—you can refer to such bindings
wherever you want. These are called global.

But bindings created for function parameters or declared inside a
function can be referenced only in that function, so they are known
as local bindings. Every time the function is called, new instances of
these bindings are created. This provides some isolation between
functions—each function call acts in its own little world (its local
environment) and can often be understood without knowing a lot about
what’s going on in the global environment.

Bindings declared with let and const are in fact local to the block that
they are declared in, so if you create one of those inside of a loop, the
code before and after the loop cannot “see” it. In pre-2015 JavaScript,
only functions created new scopes, so old-style bindings, created with
the var keyword, are visible throughout the whole function that they
appear in—or throughout the global scope, if they are not in a function.
let x = 10;
if (true) {
let y = 20;
var z = 30;
console.log(x + y + z);
// → 60

Written for

Course

Document information

Uploaded on
May 1, 2023
Number of pages
20
Written in
2022/2023
Type
SUMMARY

Subjects

$13.09
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
lavanyaa2304

Get to know the seller

Seller avatar
lavanyaa2304 SUBBIAH VIDYALAM GIRLS HR SECONDARY SCHOOL
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions