The Basics of JavaScript

Part 2: Program Structure and Functions

Hemanth Raju
3 min readDec 14, 2022

Hi all! In the last article, I have written the basics of JavaScript regarding values, types, and operators.

In this article, we are going to learn about program structure and functions.

Program Structure :

  • A fragment of code that produces a value is called an expression.
  • To catch and hold values, JavaScript provides a thing called a binding or variable:
let caught = 5 * 5;
  • The previous statement creates a binding called caught and uses it to grab hold of the number that is produced by multiplying 5 by 5.
  • The full list of keywords and reserved words is rather long.
break case catch class const continue debugger default
delete do else enum export extends false finally for
function if implements import interface in instanceof let
new package private protected public return static super
switch this throw true try typeof var void while with yield
  • A function is a piece of a program wrapped in a value. Such values can be applied in order to run the wrapped program.

Functions :

  • A function definition is a regular binding where the value of the binding is a function.
  • A function is created with an expression that starts with the keyword function.
  • A function can have multiple parameters or no parameters at all.
  • Blocks and functions can be created inside other blocks and functions, producing multiple degrees of locality.
  • JavaScript is extremely broad-minded about the number of arguments you pass to a function.
  • If you pass too many, the extra ones are ignored. If you pass too few, the missing parameters get assigned the value undefined.
  • A function that references bindings from local scopes around it is called a closure.
function add(num1){
return num2 => num1 + num2;
}
let sum = add(3);
console.log(sum(2)); //5
console.log(sum); //error
console.log(add(2)(3)) //5 ; it is alternate

In the above example, we can see that the function is returning an arrow function of adding two numbers.

So, the sum variable will get that arrow function in return, and later it will add to the argument passed to it.

  • Closure not only stores the function but also environment variables.

Recursion :

  • A function that calls itself is called recursive.
  • The main important thing in recursion is the base condition.
function fib(num1){
if(num1<=1){
return num1;
}
else{
return fib(num1-1)+fib(num1-2);
}
}
console.log(4); //2

In the above example(Fibonacci Series), if we don’t give a base condition it takes negative values and goes till -Infinity which we don’t want.

  • Recursion is not always just an inefficient alternative to looping. Some problems really are easier to solve with recursion than with loops.

Thanks for Reading my article!!!!!

Please subscribe to my email, you will get regular updates!!!

If you love my work and wanna support me, please feel free to Buy me a Coffee………. https://ko-fi.com/hemanthraju ☕️!!!!!😍

Keep smiling!!!

Have a nice day!!!

--

--

No responses yet