Modern Javascript Basics Part - II:

Modern Javascript Basics Part - II:

Basic but crucial javascript rules you should know;

ยท

9 min read

I am a javascript enthusiast, developer, teacher, and a lifetime learner. Javascript is a diverse language and a unique one considering it applies to almost all areas of development be it frontend, backend, desktop, or even mobile app development.

For this reason, I've decided to create a dedicated series of blog posts based on this language which will be all about its syntax, usability, best practices, and most importantly resources as well as lessons and experiences gained as I build my entire development career based on this language.

So join me in this quest if you want to be a lifetime learner, developer, and javascript user, and let's explore this language. You can help by commenting on your opinions, additions, experiences with the language, and most importantly share it across your social media platforms to make it more accessible and useful.

1. Javascript is case sensitive;

const username = 'Cyrus';
const Username = 'James';
let userName = 'Mark';
let UserName = 'Peter';
  • All the above variable names are considered different variables in javascript language. There are however recommended methods of declaring variables in javascript which mainly are camel case;
const   userName = 'Mark',

Using the underscore;

const  _username = 'Mathew';

and finally using the dollar sign;

const      $username = 'Brown';

2. Multiple vs Single Variable declarations in javascript;

Most javascript developers declare their variables as follows;

const username = 'Cyrus'; //varibale whose value can't be changed / altered
let userName = 'Mark'; //varibale whose value can be  altered

This method is effective in scenarios where there is a limited number of variables to be declared at a particular instance in your code. But it's possible to simplify the code especially when there are multiple variables to be declared by grouping them as follows;

//Varibales whose value cant be changed or altered
const username = 'Cyrus',
    pi = 3.142,
    website = 'cyrusCodes.com';

//or 

//Varibales whose value can be changed or altered
let Username = 'James',
    career = 'Web developer',
    Hobby = ['Reading', 'Writing'];

Note the major differences between the two methods;

  • The first method is applicable and recommended when there are a few variables to be declared while the second is useful only when there are multiple variables to be declared.

  • At the end of every variable declaration, the first method terminates the expression with a semicolon while the second method terminates with a comma except the last expression.

All the variables declared under const are constants and cannot be changed throughout the javascript program while all variables declared under let can be altered, therefore it would be a grave mistake to combine the two types.

3. Identifiers in javascript;

Identifiers simply mean names in javascript, might be a variable name, a function name, or any other name of an object in javascript used to identify it. Examples include;

const userName = 'Mark'; //userName is the varibale identifier
const callUser = function() { // callUser is the function identifier
    console.log(`Hello ${userName}`);
};
callUser();//calling a function => Hello Mark

Several rules must apply when using the identifiers or names in javascript that include;

  • A name must start with a letter (lower or uppercase), underscore, or dollar sign but subsequent characters can be letters, digits, underscores, or dollar signs. This means that the following are the only ways allowed to start variable names in javascript;
const username = 'cyrus',
    _username = 'James',
    $username = 'Mark';
  • Digits are not allowed as first characters.
  • Special keyboard characters are not allowed for use as variable names or in variable names. Examples; (#, ^%@*).
  • Finally, there is a list of special words also not allowed which is referred to as Reserved words in javascript language which is discussed in the next item.

4. Reserved Words;

These are identifiers or names reserved for use by the javascript language itself and cannot be used to declare variables, classes, functions, or constants.

Examples of these words include; delete, import, continue, debugger, default among others all listed here.

However, even though the use is prohibited for declaration of various items in javascript which includes variables, labels, or function names, they can all be used as object properties as follows;

const user = {
    default: "cyrusCodes",
    while: "Running code",
    do: "Learn javascript",
    delete: "unused code"

};

console.log(user.default, user.while, user.do, user.delete);
//cyrusCodes Running code Learn javascript unused code

Now, I can't find any reason whatsoever one may complicate their code by doing the above since there's a rule call it silent, unsung, or obvious for programmers that states that;

For readable and maintainable code, developers should use descriptive identifiers or names to declare their variables classes or functions. This means that as much as it is not erroneous or prohibited to use unrelated names to identify your variables, it is one of the basic principles every developer should apply when writing their code.

This means avoiding declarations like these;

const a = 'cyrus',
    _b = 'James',
    c = 'Lynda';

This is because no one can tell whether the letters represent names of animals, pets, users, customers, or any other list with a bunch of names. But a descriptive declaration would be as follows;

const userName = 'cyrus',
    fatherName= 'James',
    motherName = 'Lynda';

With no doubt, one can tell exactly what the variable names are describing.

5. Javascript comments;

Javascript supports two methods of commenting mostly differentiated by the size of the comment as follows;

  • Single line comments; For single-line comments, javascript allows the use of double forward slashes as follows;
// Variable declaration
const userName = 'cyrus';
  • For multi-line comments the easiest and most effective method of comments is as follows;
    /* Variable declaration
    const userName = 'cyrus';
    */
    

This concludes the SECOND part of this amazing blog post series of basic javascript with many more to come. Click here to read the first article on this series if you haven't yet. I urge you to subscribe to this blog and get notified anytime a new part is complete.

You can also share this article on your social media to bookmark it for future reference or give access to your friends also working or interested in javascript language.

You can follow me on Twitter , where I share countless resources and articles related to javascript and we can become lifetime friends and javascript lovers.

Finally, THANK YOU for so much for taking your time to read this article. If you are feeling more generous I'll not stop you from buying me a cup of coffee. coffee_black.png

Until the next article, KEEP CODING & SHARING.