What is Javascript CallBack function and how it works ?

Published on
2 mins read

Javascript CallBacks

Img Credit : @hiteshchoudhary

Now we have a function which will take a variable as argument & that will modify the value. In this case we can sum 10 and return it.

function ModifyValue(x) {
  return x + 10
const getModifyValue = ModifyValue(10)
console.log(getModifyValue) // 20

Now if you want to do some other things like subtraction, multiplication & division. Then you have to write three separate function.

what if we can tell the function to modify the value according to our needs. Like we will pass the logic what we need instead of write same code multiple times.

function ModifyValue(x, callback) {
  return callback(x)

function sum(elem) {
  return elem + 10

function sub(elem) {
  return elem - 10

const getSumValue = ModifyValue(50, sum)
const getSubValue = ModifyValue(50, sub)
console.log(getSumValue) // 60
console.log(getSubValue) // 40

wow ! Now we can add our different Login in different function. So now the function is pass with argument thats call Callback function.

If there has any issues or wrong instruction, please feel free to contact with me by comments or mail: hello@rathik.dev