Variables? What is it?
๐ Variables are container that holds particular value for entire program execution cycle.
๐ Doesn't make any sense?
๐ Here's an example : in one of the previous blogs we thought of making a simple calculator where user can give 3 inputs like 'number 1' , 'number 2' , 'operation'.
๐ Now we need those inputs in our program every now and then...
๐ Either you can memorize your inputs๐(very unprofessional) or store it somewhere
๐ Now we have only one option , that is to store our input data into some containers
๐ In the coding world we call these containers variables . Variables are nothing but a container which stores values and reduces your work.
How can i write a variable ?
Example
var userName = "Adarsh";
Hey what's this ?
๐ Now let's break this down
๐ We've written var userName = "Adarsh"
๐ In JS var
is reserved keyword , which can not be modified , on the other side var is used for declaring a variable in our program.
๐ userName
is variable name , when you store some value in container you need to name the container to recall it later on in program, this can be anything
๐ Variable names are also known as identifiers !
๐ Rules for writing a identifier
- identifiers can not contain white spaces
- var user name
โ
- var userName
โ
- identifiers can not contain special characters except `_` and `$`
- `var user%name` โ
- `var ^username` โ
- `var user;name` โ
- `var user_Name` โ
๐ =
is also known as assignment operator , as the name suggests it is used to assign value to the variables.
JavaScript Data Types
๐ Data Types simply define what type of value is going to be stored inside our variable
๐ Example : in terms of calculator we've got 3 inputs so we need 3 variables , 2 number types (num1 and num2) and 1 for operator (text or string)
JavaScript : A loosely typed language
๐ Hey, do i have to specify data types when declaring a variable ? so , the answer is no because JS is loosely typed language !
๐ JavaScript is a loosely typed language, meaning you donโt have to specify what type of information will be stored in a variable in advance. JavaScript automatically types a variable based on what kind of information you assign to it
๐Example
var numOne = 5 // automatically assigns type `number`
var numTwo = 10 // automatically assigns type `number`
var Operator = "+"// automatically assigns type `string`
Data Types Available in JS
Primitive Data types : Primitive or primary data type simply means a data type which is given by programming language , you don't have to specify it.
Composite Data Types : as the name suggests composite data types are made up by collection of Primitive Data types.
Simple Data types
๐ Number : used for storing numbers
๐ String : used for storing text or sequence of characters
๐ Boolean : 1 bit data type which stores only True
or False
๐ Null : null is assigned value which is empty or nothing
๐ Undefined : declared variable but not defined
Don't worry if you don't get any of these , you'll understand all of these as an when we write a program :p
Let me know in comment section if you have any doubt or feedback. it's always worth to give time to thriving developer community :)
Keep Coding โค
Top comments (0)