FIRST CLASS FUNCTIONS 🔥ft. Anonymous Functions | Namaste JavaScript Ep. 13

Akshay Saini
14 Dec 202022:30

Summary

TLDRIn this educational video, the speaker recounts a nerve-wracking interview experience with a renowned Indian food-ordering startup, which led to a deep dive into JavaScript's functional programming concepts. The video clarifies the differences between function statements and expressions, explains the importance of hoisting, and distinguishes between anonymous and named function expressions. It also touches on the significance of parameters and arguments in functions. The crux of the video is the explanation of first-class functions, emphasizing JavaScript's ability to treat functions as values, allowing them to be passed as arguments and returned from other functions. This understanding is crucial for mastering functional programming in JavaScript.

Takeaways

  • 😀 An anonymous function is a function without a name.
  • 📚 First-class functions in JavaScript refer to the ability to treat functions as values, allowing them to be assigned to variables, passed as arguments, and returned from other functions.
  • 🔍 The difference between a function statement and a function expression lies in hoisting; function statements can be called before they are declared, while function expressions cannot.
  • 💡 Function declarations (also known as function statements) and function expressions are two ways to create functions in JavaScript.
  • 🚫 Attempting to create an anonymous function using the function statement syntax results in a syntax error because function statements require a name.
  • 🔑 Anonymous functions are used when functions are assigned to variables or used as values in other expressions.
  • 📝 Named function expressions are similar to function expressions but include a name for the function. However, the name is scoped to the expression and is not accessible outside of it.
  • 🤔 Parameters are the identifiers used within a function to receive values, whereas arguments are the actual values passed to the function.
  • 🌐 The term 'first-class citizens' in JavaScript refers to functions being able to be used as values, similar to any other variable or data type.
  • 👉 Arrow functions, introduced in ES6, are a more concise way to write function expressions and do not have their own `this`, `arguments`, `super`, or `new.target`.

Q & A

  • What is an anonymous function in JavaScript?

    -An anonymous function in JavaScript is a function that does not have a name. It can be used as a value and assigned to a variable or passed as an argument to other functions.

  • What are first-class functions?

    -First-class functions refer to the ability of functions to be used as values in JavaScript. They can be assigned to variables, passed as arguments to other functions, and returned from other functions.

  • What is the difference between a function statement and a function expression?

    -The main difference between a function statement and a function expression is hoisting. A function statement can be called before it is declared, while a function expression must be declared before it can be called.

  • Why is it called a 'first-class citizen' when referring to functions in JavaScript?

    -The term 'first-class citizen' refers to the fact that functions in JavaScript can be treated like any other value. They can be assigned to variables, passed as arguments, and returned from other functions, just like any other data type.

  • What is the role of hoisting in function statements and function expressions?

    -Hoisting allows function statements to be called before they are declared in the code. However, function expressions are not hoisted and must be declared before they can be called.

  • Can you provide an example of a function statement in JavaScript?

    -A function statement in JavaScript is declared using the `function` keyword followed by a name. For example: `function myFunction() { console.log('Hello, World!'); }`.

  • How do you create a function expression in JavaScript?

    -A function expression in JavaScript is created by assigning an anonymous function to a variable. For example: `const myFunction = function() { console.log('Hello, World!'); };`.

  • What is the significance of named function expressions in JavaScript?

    -Named function expressions in JavaScript are function expressions that have a name. The name is scoped to the function itself and can be used for recursion inside the function, but it is not accessible from outside the function.

  • What is the difference between parameters and arguments in JavaScript functions?

    -Parameters are the variables defined in the function declaration that receive the values passed to the function. Arguments are the actual values that are passed to the function when it is called.

  • Why is it important to understand the difference between function statements and expressions?

    -Understanding the difference between function statements and expressions is important for debugging JavaScript code and for understanding the scope and hoisting behavior of functions in the code.

  • Can you provide a scenario where an anonymous function would be useful in JavaScript?

    -Anonymous functions are useful in JavaScript when you need a function that is only used once and does not need to be referenced again, such as when passing a function as a callback or when using it as an event handler.

Outlines

00:00

😅 Interview Experience with JavaScript Concepts

The paragraph narrates the speaker's interview experience at a renowned Indian startup, where they were asked about anonymous functions and first-class functions in JavaScript. The speaker initially provided a vague answer for anonymous functions and admitted not knowing about first-class functions. This led to a nervous breakdown during the interview. The speaker then proceeds to explain function statements and expressions in JavaScript, highlighting the importance of understanding these concepts for programming and interviews.

05:00

📚 Understanding Function Statements and Expressions

This section delves into the difference between function statements and function expressions in JavaScript. The speaker clarifies that a function statement is a way to create a function with a name, while a function expression can be anonymous or named and is typically assigned to a variable. The main distinction between the two is hoisting, where function statements are hoisted during the memory creation phase, allowing them to be called before they are defined. In contrast, function expressions are treated like variables and are only assigned a function once the code execution reaches the expression line.

10:01

🔍 Deeper Insight into Anonymous and Named Function Expressions

The speaker further elaborates on anonymous functions, explaining that they cannot be created as standalone function statements due to syntax errors but can be used as values, such as being assigned to variables. Named function expressions are also discussed, where a function is given a name and used within an expression. The paragraph includes a cautionary note about the scope of named functions within expressions, highlighting that they are local to the function in which they are defined and cannot be accessed globally.

15:04

🎯 The Concept of First-Class Functions

This part of the script introduces the concept of first-class functions, emphasizing that in JavaScript, functions can be treated as values. This means they can be assigned to variables, passed as arguments to other functions, and returned from functions. The speaker clarifies that this flexibility and power of functions in JavaScript is what makes them first-class citizens. The paragraph aims to provide a clear understanding of this concept, which is crucial for any JavaScript developer.

20:06

🚀 Upcoming Topics: Callbacks and Higher-Order Functions

The final paragraph sets the stage for future videos that will cover callbacks and higher-order functions, which are integral to functional programming in JavaScript. The speaker reassures the audience that all basic concepts will be thoroughly covered before moving on to more advanced topics like arrow functions, introduced in ES6. The speaker also encourages viewers to like the video for motivation and announces the next topic to be discussed in the series.

Mindmap

Keywords

💡Anonymous function

An anonymous function is a function that does not have a name. In the context of the video, the script explains that these functions are used when functions are treated as values, such as when they are assigned to a variable or passed as arguments to other functions. The video emphasizes that while an anonymous function might look like a function statement without a name, according to the ECMAScript specification, a function statement must have a name, making a truly anonymous function statement a syntax error.

💡First class functions

First class functions refer to the ability of functions to be treated like any other variable in JavaScript. They can be assigned to variables, passed as arguments to other functions, and returned from functions. The video script uses this concept to highlight the power and flexibility of JavaScript functions, explaining that this treatment of functions as first class citizens is a fundamental aspect of the language that enables higher-order functions and other advanced programming techniques.

💡Function statement

A function statement, also known as a function declaration, is a way to define a function in JavaScript by using the 'function' keyword followed by a name. The video script explains that function statements are hoisted, meaning the function is available in the scope before it is declared in the code. This is demonstrated in the script with examples where the function can be called before it is defined without causing an error.

💡Function expression

A function expression in JavaScript is a function that is defined as an expression, typically assigned to a variable. Unlike function statements, function expressions are not hoisted, and thus, they must be defined before they are called. The video script uses the concept of function expressions to illustrate the difference in hoisting behavior between function statements and expressions, highlighting a key aspect of JavaScript's function handling.

💡Hoisting

Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase. The video script explains hoisting in the context of function declarations, where a function declared with a function statement can be called before it appears in the code. This is a critical concept for understanding the behavior of functions in JavaScript, as it affects when and how functions can be used within a script.

💡Callback function

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. Although not explicitly defined in the script, the concept is alluded to when discussing first class functions, as callbacks are a common use case for functions being passed as arguments. The video script suggests that callbacks and higher-order functions will be covered in more detail in a future video.

💡Higher order function

A higher order function is a function that takes one or more functions as arguments, or returns a function as its result. The video script mentions higher order functions in the context of first class functions, indicating that the ability to pass functions as arguments and return them is a characteristic feature of higher order functions. This concept is integral to functional programming and is teased to be covered in more depth in an upcoming video.

💡Arrow function

An arrow function is a syntactically compact alternative to a traditional function expression in JavaScript, introduced in ES6. While not the main focus of the script, the video mentions that arrow functions can also be used to create function statements and expressions. The script promises a future video dedicated to arrow functions, covering their syntax, usage, and differences from traditional function statements.

💡Named function expression

A named function expression is a function expression that is assigned a name. The video script points out the peculiarity of named function expressions, where a function is given a name but is still treated as an expression and can be assigned to a variable. This concept is used to contrast with anonymous function expressions and to illustrate the flexibility of functions in JavaScript.

💡Parameters and arguments

Parameters are the variables listed inside the function's parentheses, while arguments are the values passed to the function when it is called. The video script clarifies the difference between these two terms, which are often used interchangeably but have distinct roles within a function's scope. Understanding the distinction between parameters and arguments is crucial for correctly using and understanding function calls in JavaScript.

Highlights

Anonymous functions are functions without a name.

First-class functions in JavaScript are functions that can be assigned to variables, passed as arguments, and returned from other functions.

Callback functions are functions passed into other functions as arguments.

Higher-order functions are functions that can take other functions as arguments or return them.

Arrow functions are a more concise way to write functions in JavaScript, introduced in ES6.

Function statements are used to declare functions with a name using the 'function' keyword.

Function expressions are functions assigned to a variable, which can be anonymous or named.

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope during the compilation phase.

Function declarations (statements) are hoisted, allowing them to be called before they are defined.

Function expressions are not hoisted, and thus cannot be called before they are defined.

Named function expressions are function expressions that are named, which can help with debugging.

Parameters are the identifiers used in a function definition, while arguments are the values passed to the function.

First-class functions allow for functions to be used as values, enhancing the flexibility and power of JavaScript.

Functions can be returned from other functions, showcasing the first-class nature of functions in JavaScript.

Understanding the difference between function statements and expressions is crucial for debugging JavaScript code.

The term 'first-class citizens' refers to the ability of functions to be treated as first-class entities in JavaScript.

The video promises a future segment on arrow functions, covering their syntax and usage in depth.

The video concludes with a teaser for upcoming content on callbacks and higher-order functions, crucial for functional programming.

Transcripts

play00:00

once i was giving interview for a very

play00:01

reputed indian startup i won't take the

play00:04

name but that startup was a very famous

play00:06

online food ordering startup the

play00:09

interviewer over there asked me

play00:11

that what is an anonymous function i

play00:14

just quickly said that a function

play00:16

without a name is an anonymous function

play00:18

that was easy right then she moved on to

play00:21

the next question which was what are

play00:24

first class functions in javascript

play00:26

okay

play00:27

so

play00:28

now i got nervous

play00:30

because i never knew what first class

play00:32

functions are

play00:34

i have heard of callback functions i

play00:35

have heard of higher order functions

play00:37

i've heard of arrow functions pure

play00:39

functions but i haven't heard of first

play00:41

class functions what are they

play00:43

and i got nervous the interviewer she

play00:46

was looking directly into my eyes and

play00:48

i was gone

play00:51

i said that i don't know okay i don't

play00:53

know the answer to this question so then

play00:55

she moved on to the next question which

play00:58

was what is the difference between a

play01:00

function statement and a function

play01:02

expression

play01:04

and now that nervousness from here went

play01:07

till here

play01:08

okay i i didn't know i i didn't know

play01:11

what a function expression statement is

play01:14

so many things were going upon in my

play01:15

head

play01:16

i thought that statement expression both

play01:19

were looking like the same so when the

play01:20

nervousness went to the next level i

play01:22

firmly said that

play01:24

a statement which has functioned in it

play01:27

is a function statement

play01:29

and oh my god

play01:32

the reaction of that interviewer was

play01:34

like

play01:34

she is just going to kill me there and

play01:36

there itself she made a she made such a

play01:39

weird face i mean like if i close my

play01:42

eyes i can still like recall that face

play01:44

okay and i knew that i am like gone in

play01:47

the interview like

play01:48

finished

play01:51

so this video is dedicated to that

play01:53

interview and that interviewer so let's

play01:55

see what all these things are and uh

play01:58

let's dive deep into the code now okay

play02:02

so let's move so we will be covering all

play02:04

these topics let's go upon these slowly

play02:07

one by one okay so first of all let us

play02:09

see what a function statement is okay so

play02:13

so you know how we create a function

play02:15

there is one way to create a function

play02:17

like this so if we write a function

play02:18

keyword and we give a name so this is a

play02:21

function statement

play02:23

okay

play02:24

so if i so suppose our function does

play02:27

nothing but just a console log okay

play02:29

let's just console log and let's give it

play02:31

a call

play02:32

that's it right so this way of creating

play02:36

a function is known as a function

play02:38

statement that's it

play02:39

that's all

play02:41

okay

play02:42

and remember i have told you several

play02:44

times throughout these videos that

play02:46

functions are very beautiful in

play02:48

javascript and functions are like the

play02:50

heart of javascript right

play02:52

so

play02:53

a beautiful feature of function is that

play02:56

you can assign it to a variable also

play02:59

okay so suppose if i have a var b we can

play03:02

assign a function to it okay we can

play03:05

assign a function to it function acts

play03:07

like a value okay you might have heard

play03:10

this before i am revising it function

play03:12

acts like a value okay so you it's it's

play03:15

like a value which you are

play03:16

initializing you are initializing this b

play03:19

with a value like putting function

play03:21

this into b so this is how beautiful

play03:24

functions are in javascript in many

play03:26

programming languages you can't do this

play03:28

and people coming from like other

play03:29

languages might find this crazy but

play03:33

what this you can do okay

play03:35

let's just log something inside this

play03:37

also so let me do a console log of

play03:39

be

play03:40

called okay

play03:42

so this is known as a function

play03:44

expression and this is known as a

play03:45

function statement both are ways to

play03:47

create function and i was using this

play03:50

since many years in our programming but

play03:52

i never knew what the difference between

play03:54

these

play03:54

jargons are like and in the interview i

play03:57

i was using this right a lot in my code

play04:00

but i never knew in the interview that

play04:02

this is what it is

play04:03

right

play04:05

so

play04:06

the interviewer did not ask me what it

play04:08

is the interviewer asked me the

play04:09

difference between function statement

play04:12

and a function expression so what is the

play04:13

difference between

play04:15

these two

play04:16

so the major difference between these

play04:19

two is hoisting

play04:21

okay

play04:22

so let's first see how do you call this

play04:24

function okay so you call this function

play04:26

by just calling it like this you know

play04:28

and even in the case of function

play04:29

expression you call it like this right

play04:32

if i call it if i run this program so

play04:34

see it prints a call and it prints b

play04:36

called right so that is how we call it

play04:38

but the difference between creating

play04:40

functions with these two types is

play04:42

hosting

play04:43

so how does hoisting come into picture

play04:46

so what if i call this function a

play04:50

even before creating it

play04:52

okay so

play04:53

here i am creating the function but i

play04:55

want to call it before creating it and

play04:58

similarly for b also

play05:00

so you have seen my hosting video right

play05:02

so what do you think the output will be

play05:04

if i call these functions before even

play05:06

creating them in code what will happen

play05:09

can you give it a guess

play05:11

yes let's just see so one will throw out

play05:13

an error which one the b

play05:16

so that is the difference between a

play05:17

function statement and a function

play05:19

expression so during the hosting phase

play05:22

during the memory creation phase a is

play05:24

created a memory and this function is

play05:27

assigned to a

play05:28

but in case of a function expression

play05:30

this b is treated like any other

play05:32

variable

play05:33

it is assigned undefined initially

play05:36

until the code hits this line itself

play05:39

so when the javascript engine executes

play05:41

this line by line and it reaches this

play05:44

line then only this function is assigned

play05:47

to this variable b until then it is

play05:49

undefined

play05:51

okay so here the value is undefined and

play05:53

you can and you can't call that b right

play05:56

that is the major difference between

play05:57

function statement and a function

play05:59

expression so now let's move on to

play06:01

function declaration so function

play06:03

declaration is nothing this is another

play06:05

jargon and function statement is also

play06:08

known as

play06:09

function declaration okay so they both

play06:12

are the same thing if you say

play06:14

function declaration or a function

play06:16

statement these are both the same things

play06:18

so now let's move on to the anonymous

play06:20

functions so anonymous functions as you

play06:22

know a function without a name is an

play06:24

anonymous function

play06:26

this is an anonymous function

play06:28

okay

play06:29

but let me tell you a little more about

play06:31

anonymous functions so anonymous

play06:33

functions

play06:35

does not have their own identity okay

play06:38

when i say it does not have its own

play06:39

identity that means if you create an

play06:41

anonymous function like this this will

play06:44

result out to be a syntax error okay so

play06:47

via syntax error let me tell you an

play06:50

anonymous function looks like a function

play06:51

statement right it looks like similar to

play06:54

that right see if you clearly see this

play06:56

looks exactly same as a function

play06:57

statement but it has no name

play07:00

but according to ecmascript

play07:02

specification a function statement

play07:03

should always have a name so this is a

play07:05

invalid syntax let me run and show it to

play07:07

you so if i save this run this so this

play07:10

is c a syntax error and clearly see the

play07:13

message which it says a function state

play07:15

function statements require a function

play07:17

name

play07:18

okay so see it's a function statements

play07:21

okay this function statements it is type

play07:23

of a function statement and it requires

play07:26

a name okay you need to give a name over

play07:28

here you can't leave it anonymous and

play07:30

create anonymous functions like this

play07:32

so now when you can't create it like

play07:34

this what is the use of it

play07:36

so anonymous functions are used in a

play07:38

place where functions are used as values

play07:42

so i have said this a lot of times let

play07:44

me repeat it once again functions are

play07:46

like very beautiful in javascript i love

play07:48

functions and functions i think are

play07:51

heart of javascript they are so powerful

play07:53

you can use them as values also right

play07:57

and anonymous functions are used when

play07:59

the functions are used as values okay

play08:02

when they are used as values so when i

play08:05

say they are used as values that means

play08:06

that you can use use it to assign it to

play08:10

some variable so just like you assign

play08:11

any other value to a variable okay you

play08:14

can assign this anonymous function also

play08:16

to a variable

play08:18

so it acts like a value here you can use

play08:20

anonymous functions but in function

play08:23

statements you cannot use an anonymous

play08:25

function okay so that is why it says

play08:27

function statements

play08:29

require a function name okay you know

play08:31

this knowledge of knowing what is a

play08:33

function statement or expression a

play08:35

declaration and all these things is very

play08:37

important also it helps you a lot if you

play08:39

are debugging programs so if you read

play08:41

clearly the message right so function

play08:43

statement so here it's not an expression

play08:45

it's a statement so these things these

play08:48

keywords will kind of click your mind

play08:50

and you will better understand

play08:51

javascript

play08:53

and generally you might have seen that

play08:54

when we are reading blog articles or we

play08:56

are reading books sometimes

play08:58

we it is very confusing right the author

play09:01

uses the term expression somewhere it is

play09:03

a statement somewhere so this is what it

play09:05

means okay so this knowledge will be

play09:07

very helpful when you are reading some

play09:09

books of javascript some blogs of

play09:11

javascript some stack overflow answers

play09:12

this all knowledge will be very helpful

play09:14

for that so now let's just see what is a

play09:16

named function expression okay so let me

play09:18

just first of all remove these errors

play09:20

okay let me just comment it out and

play09:23

let me bring these calls uh over here

play09:26

once again

play09:28

so now let me tell you what is a named

play09:30

function expression so a named function

play09:32

expression is like exactly the same as

play09:35

this function expression but when this

play09:37

anonymous function instead of using an

play09:39

anonymous function here we use a

play09:41

function with a name suppose if i gave

play09:43

it a name so this becomes a named

play09:45

function expression

play09:47

it's kind of weird right

play09:49

you are like kind of giving a name to

play09:51

the function here also and you are

play09:53

giving the name to the function here

play09:55

also it's it's like weird right but this

play09:58

is possible in javascript like you can

play10:00

have the name of a function itself and

play10:02

then put it into an expression so this

play10:04

is known as a named function expression

play10:07

remember a named function expression now

play10:09

here there is a corner case also there

play10:11

is a gaucha okay so uh so you know we

play10:15

can call this b function like this but

play10:18

what do you think what will happen if i

play10:20

do something like this x y z so can you

play10:22

guess what will happen if you just call

play10:24

this function x y z like this give a

play10:26

guess

play10:27

so you will get an error okay so it is a

play10:29

very famous output question also so you

play10:32

might get a mcq where

play10:34

the person might have called this xyz

play10:36

like this but it will throw out to be an

play10:39

error okay so this will give a reference

play10:41

error that xyz is not defined okay so

play10:43

why it gives xyz is not defined so

play10:47

if we see clearly this x y z if you put

play10:50

a function like this right if you use

play10:52

this function as a value so in this case

play10:55

this x y z is not created in the

play10:58

outer scope okay so this xyz is not a

play11:02

function inside this outer scope in this

play11:04

close global scope but it is created as

play11:07

a local variable okay when i say a local

play11:09

variable if you try to if you want to

play11:11

access this function inside this

play11:13

function okay if you want to access this

play11:16

function inside this function

play11:19

okay so suppose if you want to access

play11:21

xyz you can access it over here right

play11:25

you can access this function see it

play11:27

prints xyz and you can access this

play11:29

function over here but if you try to use

play11:31

it outside like this it will throw out

play11:33

to be an error where it is okay let's go

play11:35

to console so it it gives you a

play11:37

reference error that xyz is not defined

play11:39

okay so this is what is called as a name

play11:42

function expression and if you just

play11:44

don't give a name over here it is like a

play11:46

normal function expression or you might

play11:48

call it as an anonymous function to

play11:50

create a function expression okay so

play11:53

something like this so let me just

play11:55

revert it once again

play11:57

um and just remove this

play11:59

xyz

play12:00

and let's move on to the next part okay

play12:03

one more thing which i have often seen

play12:05

is that

play12:06

a lot of programmer use parameters and

play12:08

arguments as interchangeably terms but

play12:11

no they are very different okay so these

play12:14

two things parameter and arguments are

play12:16

very different okay so whenever you are

play12:18

creating a function so whatever you put

play12:21

over here right these these identifier

play12:24

or this labels right

play12:26

over here are known as parameters so

play12:28

that is why you can call it as a param

play12:30

param1 okay so you might have seen the

play12:32

syntax written a lot of places so this

play12:36

identifier or the label

play12:38

or this variable okay this is a local

play12:40

variable in the function scope okay so

play12:43

this this param1 and param two are local

play12:46

variables inside this function you

play12:47

cannot access this param1 and param two

play12:50

outside okay so these local variables or

play12:53

you can call it them as the identifiers

play12:56

or the labels so whatever you might call

play12:57

them so these are the parameters

play13:01

parameters of a function

play13:03

and the arguments which you pass over

play13:05

here like if i uh give a one and a two

play13:08

so this is known as arguments a lot of

play13:11

people you know

play13:13

use this as interchangeably and don't

play13:15

know this that is why often when you

play13:17

read a blog or you read a book then

play13:19

somebody is using an argument somewhere

play13:21

if somebody using parameter so you

play13:23

should actually have a mental model of

play13:25

what that person is talking about that

play13:28

is when you will understand these blogs

play13:30

and articles and books right

play13:32

so once again the values which we pass

play13:34

inside a function are known as arguments

play13:37

and

play13:38

these label and identifier which gets

play13:41

those values are known as parameters

play13:43

okay so now let's move on to the most

play13:45

critical part which is first class

play13:47

functions okay

play13:49

so what are first class functions so

play13:51

this is a very heavy term for what we

play13:53

already know

play13:54

so as i already told you and let me

play13:56

repeat it once again because it is very

play13:58

important functions are very beautiful

play14:01

in javascript functions are very strong

play14:03

functions are heart of javascript

play14:06

and

play14:07

just because it is like that

play14:09

we call them as a first class functions

play14:12

so the functions are so beautiful so

play14:14

beautiful that instead of these

play14:17

arguments we can even pass functions

play14:20

inside another functions as an arguments

play14:24

okay listen to this carefully arguments

play14:27

so functions are treated as values right

play14:29

i told like you can use an anonymous

play14:32

function to pass in as a value also okay

play14:34

that is how beautiful functions are and

play14:37

you can receive that in the parameters

play14:39

okay so if i pass this anonymous

play14:41

function inside param1 and i try to

play14:44

access this panel and see what we get

play14:46

okay this is a perfectly valid

play14:48

javascript okay see we got this

play14:50

anonymous function inside it right and

play14:52

there is one more way to pass this

play14:54

function

play14:55

okay inside this function okay suppose

play14:57

it was a named function let's call it as

play15:00

uh x y z okay and if i pass this x y z

play15:03

like this we can do that too okay so

play15:06

it's like passing another function

play15:09

inside a function right we are passing

play15:11

this x y z inside b

play15:13

that's how beautiful functions are and

play15:15

we can do that

play15:16

and not just this if we pass this

play15:19

function into this function we can also

play15:22

return a function from a function

play15:24

okay crazy it sounds right so we can

play15:28

return a function from a function we can

play15:30

return an anonymous function from a

play15:32

function

play15:33

and what will happen is when you invoke

play15:35

this b right when you call this b

play15:39

this function will be returned over here

play15:42

right so let's just try to do a console

play15:44

log of this okay so if i do a console

play15:47

log of this

play15:48

so uh and

play15:50

let me just remove this x y z

play15:53

and let me just remove this x y z also

play15:56

and if i now

play15:58

run this code so see

play16:00

when we call this function so

play16:03

this function anonymous functions were

play16:04

returned from b and we did a console log

play16:07

so it prints an anonymous function and

play16:09

if it was a named function we can return

play16:11

that too okay so what is first class

play16:14

functions so first class functions is

play16:16

nothing but this only

play16:18

the ability to use functions as values

play16:21

is known as first class functions

play16:23

so let me repeat it once again the

play16:25

ability of functions to be used as

play16:27

values and can be pass this in an

play16:30

argument to another functions and can be

play16:32

returned from the functions

play16:35

is this ability is known as first class

play16:38

functions in javascript

play16:40

nothing more than that okay these

play16:42

ability of these functions to use an n

play16:44

value and assign it to a variable and

play16:47

can be passed into another functions and

play16:49

can be returned out of another functions

play16:51

this ability all over all together is

play16:54

known as first class functions

play16:56

that is what is first class functions

play16:58

and it's not just in javascript it is a

play17:00

programming concept it's in other

play17:02

languages also not in every language but

play17:05

in many languages also so our interview

play17:08

question can be what are first class

play17:10

functions in javascript so you have to

play17:12

explain it like this okay when they are

play17:14

treated as a value parsed into another

play17:16

functions or returned from another

play17:18

functions so this is known as a first

play17:20

class functions okay this ability is

play17:23

known as the first class functions

play17:25

that's all it is

play17:26

right so if you read blog articles or if

play17:29

you read books okay so a lot of times

play17:31

you might also find a statement which is

play17:33

functions are first class citizens let

play17:36

me repeat it once again functions are

play17:38

first class citizens okay so let me

play17:42

write it down first class

play17:44

citizens okay

play17:46

so

play17:47

when i say first class citizens it means

play17:50

the same thing

play17:52

first class functions functions are used

play17:55

as first class citizens so it's both the

play17:58

same thing okay so if you read it in

play18:00

some blog or if you read it in some book

play18:02

assume that this ability to be used like

play18:05

values

play18:06

makes the function as first class

play18:08

citizens in javascript i know this is

play18:11

kind of like a playing with english and

play18:13

words right but that's how it is and it

play18:16

is important to know all these things

play18:18

because if we read it in some book we

play18:20

should understand what that author is

play18:22

trying to say right

play18:23

javascript has a very vast community

play18:26

right a lot of people are writing stack

play18:28

overflow answers blogs blogs articles

play18:31

this that and they use all sort of

play18:33

notations so what i try to do is i am

play18:36

just giving you this knowledge so that

play18:37

next time you read these blog articles

play18:40

you will properly understand what that

play18:41

author is trying to say if somebody says

play18:43

that functions are first class citizens

play18:46

that means he is referring to first

play18:48

class functions that means the ability

play18:50

of these functions to be used as values

play18:52

passed inside another function get out

play18:55

from the function and like returned out

play18:57

from the functions use them to assign

play18:59

into a variable all these things make

play19:02

say functions as the first class

play19:04

citizens in javascript

play19:06

remember will you remember this lifetime

play19:08

remember this lifetime okay so one more

play19:11

doubt you might get is that what if i

play19:12

use a letter const over here so suppose

play19:15

if i use a letter const over here so in

play19:17

that case also it behaves the same way

play19:20

just like let and const normal variables

play19:22

do they are in a temporal dead zone

play19:25

until it encounters this statement okay

play19:28

so you it is treated exactly the same if

play19:30

you want to get more information it is

play19:32

in the latin cons video right go watch

play19:34

that again if you are still confused

play19:36

about it so in the latin const video

play19:38

whatever rules are applied the same

play19:40

rules are applied to these also so these

play19:43

are nothing this const b is nothing but

play19:46

just like a constant another variable

play19:48

and this function over here is acting

play19:51

just like any other value which is being

play19:53

assigned to this b okay so that's how

play19:56

and that's what it is okay

play19:58

so now this function statement in a

play20:00

function expression or a function

play20:01

declaration these all things can also be

play20:03

written using arrow functions okay arrow

play20:06

functions has come up as a part of es6

play20:09

you can call it as ecmascript 2015 and

play20:12

this let const arrow functions these

play20:15

things were introduced in es6 okay so

play20:18

these function statements function

play20:20

expression this all things can be

play20:21

created using arrow functions also okay

play20:24

but let's not do this in this video

play20:26

because i haven't told you what an arrow

play20:28

function is

play20:29

up till now in this series i have seen a

play20:31

lot of people asking in comments that

play20:34

what will happen in case of arrow

play20:35

functions error functions error

play20:36

functions don't worry people

play20:38

i am covering the basics first of all

play20:41

okay the basics of javascript because

play20:43

when javascript was built it was not

play20:45

built with arrow functions right it used

play20:47

to have where it used to have function

play20:49

keyword so let us first explore these

play20:52

basic nitty gritty things about uh these

play20:55

this beauty of javascript which was

play20:57

originally made right and we will slowly

play20:59

transition towards this es6 new things

play21:02

also you know i am not avoiding let

play21:05

const or

play21:06

arrow functions or something because

play21:08

those things are being used a lot right

play21:11

i will make a separate video all

play21:12

together just for covering arrow

play21:14

functions that will be a very

play21:16

interesting video i will be covering

play21:18

everything like how how this keyword

play21:21

works and how arrow function syntax

play21:23

works how higher order function works

play21:25

and how

play21:26

statements declaration expression

play21:29

everything we will be covering in arrow

play21:30

function series just keep calm and wait

play21:32

no issues everything will be covered

play21:34

slowly slowly slowly slowly slowly

play21:36

slowly slowly slowly slowly slowly and

play21:39

trust me i'm not going anywhere i'll

play21:41

teach you everything just keep on

play21:43

watching this series and watch it in the

play21:45

sequence okay in the next video we will

play21:47

be covering callbacks and higher order

play21:49

functions those are very very very

play21:51

important topics in javascript if you

play21:52

want to learn functional programming

play21:55

okay so don't miss that video that is a

play21:57

very crucial video but before moving on

play21:59

to that video give this video a thumbs

play22:01

up it gives me a huge motivation and see

play22:03

you in the next video till then thank

play22:05

you for watching namaste javascript

play22:08

[Music]

play22:24

[Applause]

play22:27

me

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
JavaScriptCoding TutorialFunction TypesFirst-Class FunctionsInterview TipsProgrammingWeb DevelopmentES6 FeaturesFunctional ProgrammingTech Education
¿Necesitas un resumen en inglés?