DEV Community

Cover image for Why JavaScript is confusing?, Learn it by knowing the difference
Mahmoud Galal
Mahmoud Galal

Posted on

Why JavaScript is confusing?, Learn it by knowing the difference

JavaScript is a superstar those days with its friend typescript

They have a lot of libs and their community is really big, and this made it very useful as there are a lot of frameworks.

So you can create everything with JavaScript, mobile apps, desktop, web apps, even AI models, IoT, Backend and Frontend.

Those reasons maybe make you learn it.


Why it’s confusing for us?

really this has a lot to know, but mainly the big difference is the syntax, and some other functionality, I will cover all of them here.


Declaring a variable

In other languages you know there’s a primitive Data Types

like int,char,string,float,etc.

but JavaScript have something else to say about Declaring Variables

they called it dynamically typed, and that means JavaScript will auto-understand what is inside this variable, like Python.

let x=5;// int
let str='hi' // string with single quote
let Str="Hi" // string with double quote
Enter fullscreen mode Exit fullscreen mode

There’s no char here the string can be one character as you want.

Other datatypes like boolean

let status=true;
let isWorking=false;
Enter fullscreen mode Exit fullscreen mode

What is confusing in Declaring a variable

there’s an old-school keyword to declare a variable var
This keyword make a confusion
let’s see

{
// making a local scope with brackts
let x=6;
var y=5;
}
console.log(x);// undfinded 
console.log(y);// 5
Enter fullscreen mode Exit fullscreen mode

that’s happened because var is a globally-scoped variable

it’s seen everywhere

to fix something like that we use ‘use strict’ I will talk about it soon.

'use strict'
{
// making a local scope with brackts
let x=6;
var y=5;
}
console.log(x);// undfinded 
console.log(y);// undfinded
Enter fullscreen mode Exit fullscreen mode

Functions

declare a function is everywhere but here’s the difference

you can pass any type of value to the function

function foo(x,y){
 return x+y;
}
foo("Hi"," Kro") //Hi Kro
foo(2,4) // 6
Enter fullscreen mode Exit fullscreen mode

maybe you see that’s cool but in many cases isn’t cool cause it will make your function fall into bugs, you have to be careful when you deal with it.

The ==

really there’s another comparison operator is ===
yeah you see

the difference is really because the variable

as you know we can declare anything using let

so when we compare we may compare with different data types

let x=5;
let y="6";
(x==y)// true
(x===y)//false
Enter fullscreen mode Exit fullscreen mode

the == operator is just to see if they are the same without begin aware if they are different data types or not

but the other one, === want the exact match in the look and the data type

Things to avoid

As we know JavaScript does not require a semicolon to end your statement

but it must sometimes,

Why?

really because JavaScript parser or engine ( similar to the compiler )

put a semicolon on every good ending expression

alert(5+
6
+2); // 13
Enter fullscreen mode Exit fullscreen mode

As you can see here Js parser didn’t put a semicolon in this case, cause when he sees no good ending expression will ignore it.

for more tutorials about JavaScript

The Modern JavaScript Tutorial

Top comments (0)