Function vs. Const

Hi Folks,

Forgive my ignorance but I’m having some trouble understanding when I should use ‘function’ and when I should use ‘const’. Could someone explain it to me please?

Thank you so much!


Hey there @halesrus7 :grinning:

const is used to create a variable that can not be changed, say a permanent password:

const password = "Super Secret Password";

It is simply a way to store a value. You can than obtain its value or compare its value letter:

if (userInput === password) {
  console.log('That is correct');

On the other hand function is used to create a section of code that can be reused many times:

function add(n1, n2) {
  let result = n1 + n2;
  return result;

You can than call a function later in your code like so:

let number = add(5, 6);
//This will log 11
//and create a new variable named number equal to 11

Thank you so much! I haven’t been able find anything that laid it out that clearly with examples. Thank you thank you thank you!

1 Like

Sorry on my part, I forgot to mention the use of const in arrow syntax.

Arrow syntax is a method of writing more concise function using the const statement.
Here is a regular function versus an arrow function, notice there is less space is taken up by the arrow function:

function add(n1, n2){
  return n1+n2;


const add = (n1, n2) => n1+n2;

you than can use it the same way as you would a normal function:

let number = add(5, 6) //number is equal to 11

Do note that arrow syntax was added in the ES6 version of JavaScript and is not supported in ES5.

1 Like

We can, and probably should use const on function expressions, too.

const foo = function (bar) {
    // code to do with bar

This protects the function from deletion or change.


Hi sir
I am learning call back function –
can you please explain [ callback() ] in first function. I don not understand why its here it is a argument or parameter .

function add(a, b , callback){
console.log(The sum of ${a} and ${b} is ${a+b}. );

// disp() function is called just
// after the ending of add() function
function disp(){
console.log(‘This must be printed after addition’);

// Calling add() function

The sum of 5 and 6 is 11.
This must be printed after addition

function add(a, b , callback){

callback is the formal parameter, which is expected to be a function reference.

add(5, 6, disp);

disp is the argument, the expected function reference.


okay got that Thank you sir

1 Like

Hello sir,
can you please help me here , in learning Nested Objects

let spaceship = {
passengers: [{name: ‘Space Dog’} ,{age: 35}], // please help me to understand this line

// can we write like this ?? :arrow_down:
name:‘Space Dog’
age: 35**

telescope: {

yearBuilt: 2018,

model: "91031-XLT",

focalLength: 2032 


crew: {

captain: { 

  name: 'Sandra', 

  degree: 'Computer Engineering', 

  encourageTeam() { console.log('We got this!') },

 'favorite foods': ['cookies', 'cakes', 'candy', 'spinach'] }

} };

No, because there is more than one passenger. Each one will need an object with the same property names.

passengers = [
    {name: 'Space Dog', age: 35},
    {name: 'James Kirk', age: 40},
    {name: 'Arthur Dent', age: 55}
1 Like

It means if we using more than one objects with the same name properties in parent object than we using this parentheses[ ] method. ?
thank you sir

The [] denotes an array which contains one object or value per index position. We can now refer to the anonymous objects by their index.

// {name: 'Arthur Dent', age: 55}

It represents as an object so now we know the data access points…

passengers[2].name    //  Arthur Dent
passengers[2].age     //  55
1 Like

more easy and understandable
thank you sir

1 Like

You’re welcome.

I meant to add, ES6 gives us the of operator to poll values in iterables. We don’t need the index if we just want to parse the whole list.

for (let x of passengers) {
    console.log(`${}, ${x.age}`)

This is a read-only loop

1 Like

can you pls explain the difference of . … calling function… in the end of both code.
1 - alienShip.retreat(); 2 - greenEnergy(spaceship);
why first one with obj . retreat() and second object passed as argument

let retreatMessage = ‘We no longer wish to conquer your planet. It is full of dogs, which we do not care for.’;

let alienShip = {





 console.log('Spim... Borp... Glix... Blastoff!');


}; alienShip.retreat();



let spaceship = {

‘Fuel Type’ : ‘Turbo Fuel’,

homePlanet : ‘Earth’


// Write your code below

let greenEnergy = obj =>{

obj[‘Fuel Type’] = ‘avocado oil’;

} ;

let remotelyDisable = obj =>{

obj.disabled = true;




What is it you do not understand?



why first one is ---- objectName.methodName();

second ---- methodName(objName);
can we call them in vice versa way

Because the first one is a method of the object

the second is a function which takes an object as argument.

1 Like