DEV Community

boopalan
boopalan

Posted on

Variable in javascript

javascript is a dynamically typed programming language, so we do not need to use the type while using the variable values.

Variable declaration?

First we want to define the variable name, only defining a variable is a declaration. Later you can give the values to those variables
Enter fullscreen mode Exit fullscreen mode

example

variableDecleration;

Enter fullscreen mode Exit fullscreen mode

*declaration and initialisation *

suppose if you define and give the values in the same line then it is said as both the declaration and initialization.

example

username = ajith;
Enter fullscreen mode Exit fullscreen mode

Later we can use this variable wherever we want to use the values in the variable.

Keywords in variable declaration.
Enter fullscreen mode Exit fullscreen mode
Simply we can declare a variable without any keyword like the above one. But it is not a preferred way to create variables in javascript. There are some preferred ways to declare variables in javascript. Let We see them one by one 
Enter fullscreen mode Exit fullscreen mode

*using var keyword *

It is the initial keyword used in javascript to define variables. To define variables but it caused many issues in the later part of the code. Let we see them how it case problem in later part of the code
it allows the variables to be defined several times, it reduces the code quality of the code.

Example

    var zero = 0;
    //  code 
    // if use define the code once again it again create the variable again 
    // defining the same variable again and again is not good code, it reduces the code quality.
    var zero = hello
Enter fullscreen mode Exit fullscreen mode

to reduce the issues and drawbacks in the var keyword they created the let keyword.

Let stops the develops to define the variable again

Example

let zero = 0;
    //  code 
    // if use define the code once again it again create the variable again 
    // defining the same variable again and again is not good code, it reduces the code quality.
    let zero = hello
    // this time javascript would not allow the developers to define once again 

but it allows the developers to change the values of the variable

example
    let username = user1
    username = adminuser // it allows to change the values 

Enter fullscreen mode Exit fullscreen mode

Sometimes we do not need to change the values again. For those cases they introduce another keyword.

Const keyword
it does not allow the developers to again and again, also it does not allows to change the values of the variables those using the const keyword

example

const isAdmin = true;
    const isAdmin = false; // it does not allow to change the values again
    isAdmin = 0; it does not allow the define again ;

Enter fullscreen mode Exit fullscreen mode

arithmetic operation in javascript

addition

10 + 4 = 14
30 + 10 = 40

exmple

let a = 20;
let b = 30;
let c = a + b;
console.log(c) // 50
Enter fullscreen mode Exit fullscreen mode

in other types of datas

“10” + 1 = 101

subtraction

15 -5 => 10
5 -15 => -10

let a = 20;
let b = 30;
let c = a - b;
console.log(c) // -10
Enter fullscreen mode Exit fullscreen mode

suppose if we have other then number casues this kind of issues
‘15’ -5
10 + “5”
10 + true

here it will try to convert the types two different types into same time, for that it will convert the sting to number, here we are given the number as a sting and we got the result

suppose if we define the a string as a number what will happen
‘abc’ -5

it will try to convert the non numbers to number, here the ‘abc’ are not number so it say ‘NaN’

and NaN + number is NaN

Top comments (0)