Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 870

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T02:53:08+00:00 2024-11-25T02:53:08+00:00

Modern Javascript Basics – Part III

  • 62k

1. Drop the use of var in javascript ;

There are various reasons why you should drop the use of var in javascript mostly related to scope as expounded in this article.

Basically, var has a lot of “sinkholes” that have proven to be quite a nightmare especially when used in production code, and it's advisable to instead use let or const. Have a look at this article that differentiates between let and const.

Did you know you can declare the keyword let as a variable using var? ie;

var let = 'cyrusCodes'; 
Enter fullscreen mode Exit fullscreen mode

Now far from the logic of this, the fact that javascript allows it should scare you away from ever using this keyword for declarations. var basically ignores the cardinal rule of not using reserved words as identifiers, You can find more rules to remember here. I thought that's another good reason why you should drop the use of var and avoid these possible complications.

2. Null vs undefined in modern javascript ;

  • Null is one of the primitives types in javascript and basically means the absence of value.

  • undefined means that the variable has never been initialized or declared in the program.
    To elaborate on this, we will use an example;

console.log(name);//ReferenceError: name is not defined 
Enter fullscreen mode Exit fullscreen mode

This means that our variable name does not exist whatsoever, which should not be mistaken with the primitive type we are discussing of undefined. This is just a javascript error stipulating that we should declare our variables before use.

However, if we correctly declared the variable but failed to give it a value like this;

let names; console.log(names); //undefined 
Enter fullscreen mode Exit fullscreen mode

Then what we have is no longer an error but a primitive type of undefined. This means that the program is aware that the variable exists i.e. it's declared, but it's not assigned to any value or initialized. To confirm this we can use typeof as follows;

let names; console.log(typeof(names)); //undefined 
Enter fullscreen mode Exit fullscreen mode

We can also treat undefined as a value of variables that have not been initialized which means assigning our variable to the value of undefined as follows;

let names = undefined; console.log(typeof(names)); //undefined 
Enter fullscreen mode Exit fullscreen mode

This is unnecessary since declaring our variable without assigning it a value or initializing it gives the same result as we've already covered.

Undefined can also be a result of several operations in javascript which include and not limited to;

  • The value of an object property that does not exist;
let names = {}; console.log(names.age); //undefined 
Enter fullscreen mode Exit fullscreen mode

  • value of a non-existent array element. Example;
let names = []; console.log(names[0]);//undefined 
Enter fullscreen mode Exit fullscreen mode

  • Functions that do not explicitly return a value Example;
function user(name) {  } console.log(typeof(user())); //undefined 
Enter fullscreen mode Exit fullscreen mode

  • Finally the value of function parameters for which no argument is passed as follows;
function user(name) {     console.log(name); } user(); //undefined 
Enter fullscreen mode Exit fullscreen mode

Null on the other hand means no value. It's an assignment value and also an object. This means we can assign our variable to null as follows;

let names = null; console.log(names); //null 
Enter fullscreen mode Exit fullscreen mode

If we check the type of null in javascript we get the following results;

let names = null; console.log(typeof(names)); //object 
Enter fullscreen mode Exit fullscreen mode

When it comes to comparing between the two primitive types;

  • The equality operator considers then equal. Example;
console.log(null == undefined); //true 
Enter fullscreen mode Exit fullscreen mode

This simple snippet may have serious implications in your code where you may get the result of one while expecting the other. Example;

let user = null; let users = undefined; console.log(user == users);//true 
Enter fullscreen mode Exit fullscreen mode

As already discussed, we both know the two are not the same or equal but when you use the equality operator to compare the two in your code, you may spend a significant time trying to find the problem.

  • That is why To distinguish the two, it's advisable to use the strict equality operator since it can differentiate the two as follows;
console.log(null === undefined); //false 
Enter fullscreen mode Exit fullscreen mode

which is the same case in your code as follows;

let user = null; let users = undefined; console.log(user === users); //false 
Enter fullscreen mode Exit fullscreen mode

In javascript, it's important to differentiate between =(Assignment operator), ==(Equality operator),===(Strict equality operator) and know where best to apply each of these tools. If you are really interested read this article and you'll be surprised at how different and significant each of these tools is in javascript language.

Finally, neither null nor undefined has any properties or methods associated with them, and trying to use any in javascript will result in a type error. Examples;

console.log(null.length); //TypeError: Cannot read property 'length' of null console.log(undefined.length); //TypeError: Cannot read property 'length' of null 
Enter fullscreen mode Exit fullscreen mode

This concludes the THIRD 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 and here to read the second article of the 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.

100daysofcodejavascripttodayilearnedwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.