JavaScript variables can hold many data types: numbers, strings, objects and more:
var length = 10;
var firstName = 'Rahul';
var xyz = {firstName: 'Rahul', lastName: 'Singh'}In programming, data types is an important concept. To be able to operate on variables, it it important to know something about the type.
| Data Type | Description |
|---|---|
| String | sequence of characters "hello" |
| Number | numeric values 100 |
| Boolean | either false or true |
| Undefined | represents undefined value |
| Null | represents null (no value at all) |
| Data Type | Description |
|---|---|
| Object | instance through which member can access |
| Array | group of similar values |
| RegExp | regular expression |
Without data types, a computer connot safely solve this:
var makeSense = 16 + "Car"Does it make any sense to add Car to 16? More importantly will it produce an error or result?
The pretiness of javascript, it will treat above examples as :
var makeSense = "16" + "Car"
// Output
"16Car"JavaScript has dynamic types. This means that the same variable can be used to hold different data types:
var x // x is undefined
x = 5; // x is number
x = "Rahul" // x is stringA string (or a text string) is a series of characters like "Rahul". Strings are written with quotes. You can use single or double quotes:
var marvelName = "Scarlett Witch"
var marvelName2 = 'Wanda Maximoff" You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
var answer1 = "It's alright"; // Single quote inside double quotes
var answer2 = "He is called 'Johnny'"; // Single quotes inside double quotes
var answer3 = 'He is called "Johnny"'; // Double quotes inside single quoteslet string = 'JavaScript'
let firstLetter = string[0] // J
// toUpperCase()
let string = 'JavaScript'
string.toUpperCase() // JAVASCRIPT
// toLowerCase()
let string = 'JavasCript'
string.toLowerCase() // javascript
// substr()
let string = 'JavaScript'
string.substr(4,6) // Script
// substring()
let string = 'JavaScript'
string.substring(0,4) // Java
// split()
let string = '30 Days Of JavaScript'
string.split() // Changes to an array -> ["30 Days Of JavaScript"]
string.split(' ') // Split to an array at space -> ["30", "Days", "Of", "JavaScript"]
// includes()
let string = '30 Days Of JavaScript'
string.includes('Days') // true
string.includes('days') // false - it is case sensitive!
// charAt()
let string = '30 Days Of JavaScript'
string.charAt(0) // 3
// search()
let string = 'I love JavaScript. If you do not love JavaScript what else can you love'
string.search('love') // 2JavaScript has only one type of numbers. Numbers can be written with, or without decimals:
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimalsExtra large or extra small numbers can be written with scientific (exponential) notation:
var y = 123e5; // 12300000
var z = 123e-5; // 0.00123Booleans can only have two values: true or false. Booleans are often used in conditional testing.
var x = 5;
var y = 5;
var z = 6;
(x == y) // Returns true
(x == z) // Returns false
var x = null
!!x // Return false
x = 5
!!x // Return True
// this one is very handy in react projectJavaScript arrays are written with square brackets. Array items are separated by commas.
var ecommerce_site = ["Amazon", "Flipkart", "Ebay", "Bewkoof"]Array indexes are zero-based, which means the first item is [0], second is [1], and so on.
JavaScript objects are written with curly braces {}. Object properties are written as name:value pairs, separated by commas.
var person = {firstName:"Rahul", lastName:"Singh", age:21, eyeColor:"black", sex:"Male"};The object (person) in the example above has 5 properties: firstName, lastName, age, and eyeColor, sex.
You can use the JavaScript typeof operator to find the type of a JavaScript variable.
typeof "" // Returns "string"
typeof "John" // Returns "string"
typeof "John Doe" // Returns "string"
typeof 0 // Returns "number"
typeof 314 // Returns "number"
typeof 3.14 // Returns "number"
typeof (3) // Returns "number"
typeof (3 + 4) // Returns "number"In JavaScript, a variable without a value, has the value undefined. The type is also undefined.
var Hotel; // Value is undefined, type is undefinedAny variable can be emptied, by setting the value to undefined. The type will also be undefined.
var Hotel = undefined // Value is undefinedAn empty value has nothing to do with undefined. An empty string has both a legal value and a type.
var hotel = ""; // The value is "", the typeof is "string"In JavaScript null is "nothing". It is supposed to be something that doesn't exist Unfortunately, in JavaScript, the data type of null is an object.
var person = {firstName:"Rahul", lastName:"Singh", age:21, eyeColor:"black", sex:"Male"};
person = null; // Now value is null, but data type is still an objectundefined and null are equal in value but different in type:
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // trueThe typeof operator can return one of two complex types:
- function
- object
The typeof operator returns "object" for objects, arrays, and null.
The typeof operator does not return "object" for functions.
typeof {name:'John', age:34} // Returns "object"
typeof [1,2,3,4] // Returns "object" (not "array", see note below)
typeof null // Returns "object"
typeof function myFunc(){} // Returns "function"Javascript also provide Math object with lot of methods to work with numbers;
Math.PI // 3.1415
Math.round(PI) // 3 to the ground values
Math.round(9.91) // 10 nearest ground values
Math.min(-5,3,4,20) // -5
Math.max(-5,3,5,20) // 20
Math.random() // generate random 0 to 0.9999 value
Math.abs(-10) // 10
Math.sqrt(100) // 10
Math.pow(3,2) // 9 i.e 3 power 2
Math.E // 2.178
Math.log(2) // 0.69314
Math.log(10) // 2.3025
Math.sin(0) // 0
Math.sin(60) // -0.3048Connecting two or more strings together is called concatenation. Using the strings declared in the previous String section:
let first_name = 'Gandhi'
let last_name = 'Rahul'
let full_name = last_name + first_name
'Rahul Gandhi'To create a template strings, we use two back-ticks. We can inject data as expressions inside a template string.
To inject data, we enclose the expression with a curly bracket({}) preceded by a $ sign.
let name = 'Rajnandini'
let place = 'Varanasi'
let dob = '10-08-1996'
`Hi ποΈ Myself ${name} and I live in ${place}`Converting one data type to another data type. We use parseInt(), parseFloat(), Number(), + sign, str().
// String to Int
let num = '10'
let numInt = parseInt(num)
numInt // 10
// String to float
let num = '9.81'
let numFloat = parseFloat(num)
numFloat // 9.81π»ππ Congrats for completing Day 1 π»ππ
if you are preparing for interview have a look at interview questions of data types