Table of contents
No headings in the article.
what are Variables & Values? why to use them?
When you want a value to be stored somewhere, you store it in a variable but why? Because to use it repeatedly in the whole program.
Let's get into some details, values are the smallest unit of information that we have in JavaScript, when we give variable a value here variable is getting declared and this is very useful in the terms of when we have to use the same value in different places in the program.
We can just use the variable in the program . If any changes we have to make in value then you don't have to go around and change the values instead you just have to change it in a single place and you are good to go.
What are const,let and var ?
If you don't want any changes in value throughout the program then in that case use const you cannot mutate the value stored here.
(If you are using const always give value to it, otherwise wise it will generate error-> Uncaught SyntaxError : Missing initializer in const declaration
)
Example : const birthYear = 1993;
.
If mutation takes place in the value throughout the program then always go forlet.
Example : let currentJob = "Teacher";
.
*var was used at the initial stage of JS , but it has some drawbacks so it was replaced with let.
Try to use const as much as possible because it avoids bugs in the longer run.
What are a conventional naming types of Variables?
Using a conventional naming type makes the variable suitable for JS to read, it is suggested to write variable names using a camelcase pattern.
Example: If you have a one-word variable:-
const first,
let first
. If you have a two-word variable write it like this :-const firstName, let firstName
.
- can also use the sign for writing a variable
$ -> dollar sign
and_ -> underscore sign
How to assign a value to variable?
Value can be of type string and can be written using single or double quotes.
Example : let firstName = 'Mark' ;
, let lastName = "Doe" ;
.
without quotes JS will consider it as a *variable.
It can be of type number and it is written like this.
Example : let marksAge = 23;
.
The variable can also store Boolean as a value.
Example : let haveDrivingLicense = True;
or let haveDrivingLicense = False;
Avoid reserved keywords when you are writing variable. You learn more about reserved keywords go to this link -> Reserved Keywords which you should not use by w3schools
Why to use descriptive way of writing variable?
Writing a descriptive variable makes it easily readable and understandable. Anyone other than you can understand the purpose of the variable and what is the value it is holding or what it is meant for.
JavaScript Fundamentals #1 That is what I have learned, for now, hope it helps someone, coming up with the next blog JavaScript Fundamentals #2.