DEV Community

Cover image for JavaScript Fundamentals: Mutable let,Comments, Functions
Astrodevil
Astrodevil

Posted on • Edited on • Originally published at mranand.com

JavaScript Fundamentals: Mutable let,Comments, Functions

Today is the 2nd day of my #100DaysOfCode journey with JavaScript.

I am going to write about my learnings in an explained way through my blogs and socials. If you want to join me on the learning journey, make sure to follow my blogs and social and share yours too. Let's learn together!🫱🏼‍🫲🏼

This Article is a part of the JavaScript Fundamentals series*.*

Mutable let

We worked on variables in the last section and now going further with variables, how can we change the stored value inside of a variable?

We used const keyword to declare a constant variable, but if we try to change the value it will give errors.

const a = 4;
a = 8;
Enter fullscreen mode Exit fullscreen mode

If we try to run above the line, it will give TypeError: Assignment to constant variable . Constants are immutable, meaning their value cannot change.

Here comes the keyword let which allows the value to be mutable (meaning it can change).

let a = 4;
a = 8;
Enter fullscreen mode Exit fullscreen mode

The above line will run without error.

Comments

Comments are an important part of programs. Writing good comments is also necessary to be a good programmer and to write clean code.

Coming up with good variable names is also important.

const value = 99;
const price = 99;
Enter fullscreen mode Exit fullscreen mode

Both hold the same value 99 but price is more descriptive.

Comments are only for humans to understand code in a better way. We can write single-line or multi-line comments.

// this is price in U.S. Dollars
const price = 99;
Enter fullscreen mode Exit fullscreen mode
/* The price of all items
   Denominated in U.S. Dollars  */
const price = 99;
Enter fullscreen mode Exit fullscreen mode

Functions

A function is a reusable code and it returns an output. The function must be defined before calling it. Function call means to execute the function. The word invoke is also used and the meaning is the same as call.

When a function is called, It means you're passing specific input values.

const output = addOne(5);
Enter fullscreen mode Exit fullscreen mode

In the code above, addOne is the function and parenthesis ( ) is used to call the function. We are passing in an input value 5 into our function addOne.

It is not mandatory to have input values in the function. See the below example.

const message = getMessage();
Enter fullscreen mode Exit fullscreen mode

In order to call a function, we must first define it!

function addOne(input) {
    return input + 2;
}
Enter fullscreen mode Exit fullscreen mode

In the above code, we are creating a function called addOne which takes one input called input. We are returning the input plus two.

function getMessage() {
    return "Hello World!";
}
Enter fullscreen mode Exit fullscreen mode

In the above code, function getMessage does not take input. We are simply returning a string, saying "Hello World!". The return statement will return the desired output of the function.

const a = addOne(2);
Enter fullscreen mode Exit fullscreen mode

Variable a is now assigned to the return value of the addOne function invoked with an input of 2, which evaluates to 4.

Conclusion

Ending with an extra bit of information about JavaScript functions...

If we define/declare a function once, it can be called elsewhere in the program.

Today I learned about Mutable let, Comments, and Functions in JavaScript.

If You ❤️ My Content! Connect Me on Twitter or Supports Me By Buying Me A Coffee☕

Top comments (0)