DEV Community

Cover image for Enough JavaScript to get you Started : #4 Data Types and Variables
Adarsh Pandya
Adarsh Pandya

Posted on

Enough JavaScript to get you Started : #4 Data Types and Variables

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";
Enter fullscreen mode Exit fullscreen mode

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` βœ…
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ = 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`
Enter fullscreen mode Exit fullscreen mode

Data Types Available in JS

Artboard – 3.png

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 ❀

Hey , Let' ConnectπŸ‘‹

Twitter /
Github

Top comments (0)