-
Notifications
You must be signed in to change notification settings - Fork 0
Functions
What does the phrase mean
Functions are first class citizens in JavaScript.
To understand this, we need to discuss programming and programs.
It is the act of describing an intent on how to solve the problem at hand.
This is usually done using a programming language.
Each programming language has basic constructs, fundamental building blocks.
Usually you want to do something, given a computer's resources.
When you express something in a language to do the thing you want to do, you have a program.
Program => Instructions + Data
Nothing more. Nothing less.
How you can write your programs, what instructions you can use, how you can treat data, what constructs are available to store different kinds of data => are described by the syntax, semantics & the constructsof the language.
Data is just values. You can imagine different kinds of data, but it all boils down to some primitive things when it comes to programming languages. In JavaScript, the primitives are identified with the following constructor functions.
- Number
- String
- Boolean
and the two distinct values
- null
- undefined
You can take these and group them together to form a collection of values.
The fundamental construct you would then use is:
- Array
You can further take these values and dream up something neat, organize a structure, a model of something, a concept, a real world entity such as a book.
The construct that lets you define such a thing would be:
- Object
In your program you can choose your values. How a programmer chooses his/her values, what he intends to do with them and how he organizes his data is all up to him & her.
Values in the program can be one off, or can be named.
For example, the code blocks
2 * 9var x = "sameeri"const pi = 3.142represent three kinds of values.
Firstly,
- 2, 9 are values.
- They are used in an expression form
- If you want to use these values somewhere else, in the program, you would have to type 2, 9 again.
In the second case,
-
xdenotes and represents the value "sameeri". -
xis a container/a bag/ a room/ a bin/ a box. - It can hold stuff.
- To put something into this container, we use the
=. -
=is anassignment operatorconstruct. - Currently this contains a
stringvalue. - But later in the program it might change, it might not.
- You can reference the value "sameeri" in any part of the program by using x.
- At any point of time, we could ask "oh! what is the value of x, here?"
- One can re-assign something else to x, such as
x=21. - That removes the value "sameeri" and puts a new value 21 into this container.
In case #3,
-
piis also a container. - You can put values in it.
- You put only one value it it.
- You put the value, and lock the container, throw the key away.
- No one can put anything else. The key is lost.
The intent of saying
I want to store this value in this container.
is called Assignment.
Assignment of values is a key operation in programs.
In JavaScript, this intent can be expressed by a single character, =.
This is a binary operator, that is, it needs two things to correctly operate.
named containers help you in repeating/reusing these values in programs.