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)