Why using "const" for functions


Can someone help me understand the term “const” better? I remember this is for variables whose value won’t change, meaning they will constantly remain the same value. However here we’re talking about a function, which is more of an action that doesn’t seem to have a value at all. so my question is, why use const here, or in other words, do we always use it when creating a function (any other syntax will be used as well). thanks.

const pressPowerButton = () => {


a constant variable can’t be renamed, this means you can’t have another variable with the same name. so you couldn’t make another function or variable with name pressPowerButton:

const pressPowerButton = () => {

const pressPowerButton = 5 // will throw error

which is a good thing, it prevents two variables/functions having the same name.


okay. so when we say “constant”, it meant for the name of the function, instead of the value to be returned from the function. correct, @stetim94 ?


pressPowerButton is a constant variable, the value of the variable is a function.

const is an abbreviation of constant.


Let me ask what I think is the same question a different way.

I’m wondering why Const is used for functions, and not, say, Function (as is done in other languages…or even, according to w3schools, its used in Javascript https://www.w3schools.com/js/js_functions.asp )
Because in other languages as I recall, constant just means a value that doens’t change. But functions…return different things so they change, right


same in javascript

the valued returned might change, but the name of the function doesn’t. So the function name is constant, and the function can’t be renamed, which is very similair to an example i did earlier:

const pressPowerButton = () => {
    if (true){
        return true;
    } else {
        return false;

const pressPowerButton = () => {
    console.log("this won't work, it will throw an error, given a constant can't be re-assigned")


The general forms, function foo () {} and var bar = function () {}; have one important difference from const foobar = () => {};, that is, the latter does not have a this context object, and has no arguments object.

It will be a little while before any of this is covered so put this in the back of your mind, for now. Bottom line, arrow notation is not a replacement for general function notation, but a labor saving device when applicable.

const makes a variable protected. It cannot be overwritten or have its datatype changed.


const foo = () => {};

foo = x => x ** 2;



SyntaxError: Assignment to constant variable: foo at 3:0.
const foo = x => x ** 2;




const only applies to the name and type of data structures such as arrays and objects. These are reference objects that are treated dynamically permitting changes in the data points and the object length.


const myArray = [];

myArray.push('foo', 'bar');

console.log(myArray);    //  ['foo', 'bar']


console.log(myArray);    //  [ 'foo', 'foobar', 'bar' ]

Notice that we are able to modify the data, but if we try to reassign to the myArray variable it will throw and error.

The method covered above is not likely covered in this course, so set it on a back burner. At some point we will be covering the push, pop, unshift and shift array methods, of which splice is similar but able to insert an element at any index, or remove an element at any index.

You will learn eventually that pop, shift and splice can be assigned to a variable or used in an expression once removed from the array.

let temp = myArray.splice(1,1);
console.log(temp);               // [ 'foobar' ]
console.log(temp[0]);            // foobar

temp = myArray.pop();
console.log(temp);               // bar

temp = myArray.shift();
console.log(temp);               // foo

Again, this is back-burner stuff but will surface eventually in your study of the language. Don’t let it segue you from the track path, but when the subject comes up. spend some time with it.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.