Coder Social home page Coder Social logo

javascript-concepts's Introduction


Concepts Every JS Developer Should Know

Concepts Every JavaScript Developer Should Know!

Introduction

This repository was created with the intention of helping developers. Use it as a guide for future studies. It is based on an repo written by leonardomso and some other resources which i used to learn JavaScript.

Community

Feel free to submit a PR adding a link to your own recaps or reviews. If you want to translate the repo into your native language, please feel free to do so.

Table of Contents

  1. Type Coercion
  2. === vs ==

1.Type Coercion

consider the following code

   [] + "2"
   
   output: "2"

In JavaScript, + can add two numbers or concatenate two strings. In this case, we have neither two numbers nor two strings. We only have one number and an empty array. According to JavaScript's type rules, this makes no logical sense. Since it’s breaking its rules, instead of crashing it tries to make sense of it anyway. So what does JavaScript do? Well, it knows how to concatenate strings, so it converts both [] and 5 into strings and the result is string value "5".

Note: == is not immune to JavaScript’s type conversion behavior.

2.=== vs ==

JavaScript has two visually similar, yet very different, ways to test equality. You can test equality with == or ===.

Double equals(==)

when using ==, Javascript compiler just checks the equality of values. it is called loose equality, Because when we are using (==) to perform equality Javascript compiler does something called type coercion.

let's illustrate this with an example

   let number = 2;
   let string = '2';
   console.log(number == string);
   
   ouput: true

== operator performs type coercion if conflicts of data types are found.

  let number = 2;
  let string = '2';
  console.log(number == string);
  
  console.log( 2 === '2' )
               ↓             // ✅ type coercion to string
  console.log('2' === '2')   // ✅ comparision of values for equality
  
  ouput: true

-[x] number variable is coerced to string from number datatype. -[x] The values are then compared for equality.

one more example to illustrate it clearly

  console.log(0 == "");
  
  output: true

How the output is logged as true 😵

Remember the steps? -[x] type coercion. -[x] comparison of values.

Type coercion is the tricky part. Both these values are converted into booleans in our above example.

   console.log( 0 ==      "");
                ↓         ↓     // ✅ type coercion to boolean
   console.log(false == false); // ✅ comparision of values for equality
   
   output: true

💩 Avoid using == when your'e comparing values for equality.

Triple equals(===)

When using triple equals === in JavaScript, we are testing for strict equality.

let's illustrate this with the same example which we used for ==

 let number = 2;
 let string = '2';
 console.log(number == string);
 
 ouput: false
  • number variable is not coerced to string from number datatype.
   let number = 2;
   let string = '2';
   console.log(number == string);
   console.log( 2 === '2')   //  ✅ comparision of data type for equality (different data types)
   
   ouput: false

the steps are as follows:

  • type checking
  • comparision of values for equality

one more example to illustrate it clearly

  console.log(0 == "");
  
  output: false

How the output is logged as false because typeof(0) is number and typeof("") is string. since the type is different the output is logged as false

 console.log(0 == ""); //  ✅ comparision of data type for equality  (different data types)
 
 output: false

one more example !?

console.log(100 == 100); //  ✅ comparision of data type for equality  (same data type)
console.log(100 == 100); //  ✅ comparision of values for equality  (same values)

output: true

javascript-concepts's People

Contributors

harishsambasivam avatar rohithchittibommala avatar yashmantri20 avatar

Stargazers

Ramesh avatar Deepesh Kumar R avatar

javascript-concepts's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.