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 ?
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.
👉 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)
👉 Hey, do i have to specify data types when declaring a variable ? so , the answer is no because JS is loosely typed language !
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
👉 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)