Day 03: Data Types and Conversion in JS
• const
• var
• let
• Data types
• Primitive data type
• Non primitive data type
• Data types conversion
• Implicit type conversion
• Explicit type conversion
const keyword in JavaScript
Const keyword
It is used to declare and initialize constant
We cannot assign a value to constant again
const x = 4.3
x = 2.3; // It will give an error
x = x + 13; // This will also give an error
You have to declare and assign the value at time of
declaration
const y;
y = 20; //error
Var keyword in JavaScript
Var keyword
Every programming language have a method to declare variable to store anything else, to process that. So in
the JavaScript, we used a var keyword to create a variable and then store data into that variable.
Example:
var b = 30;
var c =23;
document.write(b+c);
Let Keyword in JavaScript
Let
It declare a variable with limited scope
Variable will be used only that block in which it is declared
For example in specific block of code as in if else structure, function, loop etc.
function Test() {
let y = 12;
document.write(y); //give 12
}
Test();
document.write(y); // give error of not defined
JavaScript Data Types
JavaScript Data Types
• Primitive data type
The data types which is not a object having no methods are the primitive data type like Number, String, Boolean etc.
• Non-primitive data type
According to requirement, developer create object which have specific properties as well as methods, that have a data
type of non primitive data type. And we also say that it is the reference data type. As Object, Array, RegExp etc
Primitive and non Primitive
JavaScript primitive data types
String It show the sequence of characters enclose in the single or double quotes e.g. “jafricode"
Number It show numeric values e.g. 10, 21, 1.3, -34 etc
Boolean it show possible two value either false or true
Undefined it show the undefined value (variable without value)
Null nothing or not value (instance of an object)
JavaScript non-primitive data types
Object It show instance which have properties as well as function
Array It is the collection of relevant data having same data type
RegExp It show the regular expression
Type Conversion in JavaScript
Implicit type casting: Automatically convert when require in the program
Explicit type casting: Developer convert one type to another when require using function
Implicit type casting in JavaScript
Convert Data type to another data type
Case1:
“number” + null -> “numbernull”
“number” + boolean -> “numberboolean”
“number” + number -> “numbernumber”
“number” + undefined -> “numberundefined”
Implicit type casting in JavaScript
Case2:
“number” - number -> number-number
“number” * number -> number*number
“number” / number -> number/number
Case3:
‘string' - ‘string‘ -> NaN
‘numeric String' - ‘string‘->NaN
Explicit type casting in JavaScript
When we need to convert from one data type to another data type we used explicit type casting
• Strings to Numbers ->Number()
• Numbers to Strings->String()
• Booleans to Numbers->Number()
• Numbers to Booleans->Booleans()