JavaScript Basics

JavaScript Basics

JavaScript Variables & Its types.

Β·

3 min read

What are Variables?

It is like a container which stores values inside itself. It can store many types of variables. In Javascript variables are dynamic in nature means we don't give the type to variable it's assign by Javascript in the run time. We can however change the type after we assign the variable its data we want to store.

For Example:

var _variable='Name';//string type 
//after assigning other data type
_varaible=22; //integer data-type
// semicolon is used to close the line 
//'Double slash are use for comments just so you know'

// for multi line comments we can use like given below
/*
multi 
line
comment
*/

now you can see how it works so lets head over to all the js types and its uses.

Case sensitive

Javascript is a case sensitive language it means

var a=3;
//and 
var A=3;
//are not same so be careful

We cannot start any variable with number like

var 1varaible=3;

we can start variables by underscore, a $ sign or by letters. We cannot start with numbers.

Javascript reserve words cannot be used as variables name you can check below picture

JS-Reserved-Keywords.png

Var vs Let vs Const Variable Types

var are use very less it might introduce bugs into the program due to its global scope we will discuss Javascript scoping in this series of Js don't worry about it right now. Another reason for not using var is it was used before ES6 so that's also one reason for not using it.

Const are used to declare a value which you won't be changing throughout the code it can be some condition or a fix value.

Let are used to declare the block scope variables. It can be re-assigned anytime throughout code. The below picture will give you some bright picture about these three.

image.png

Javascript Data-Types!

Primitive vs Non Primitive data types

Primitive data-types are essential data types that javascript provides for declaring variables at run time.

Screenshot from 2022-09-07 17-02-37.png Seven types of Primitive data types are :

  • Null : means empty value
  • Number : Floating or number all are same
  • String : sequence of characters
  • Symbol : ES5 (2015) unique value that cannot be changed
  • Boolean : Logical data types True or False
  • Bigint : value greater than number data type can handle
  • Undefined : value taken by the variable but not yet defined or assigned anything means 'empty value' example let children;

Non-primitive data type is Object which can be declared as "like a dictionary python devs will understand but it is known as object in Javascript like key value pairs"

const item={
_var:"hello",
_price:"50"
}

if you want to find the type of variable you can just do it like this

let name="Waqar";
console.log(typeof name);

So these are just the basics of javascript variables.

Did you find this article valuable?

Support Muhammad Waqar by becoming a sponsor. Any amount is appreciated!

Β