forked from aayushyadavz/JavaScript-Full-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from AayushYadavz/main
Dataypes Summary
- Loading branch information
Showing
16 changed files
with
705 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* Data Types are divided into 2 Types, | ||
1. Primitive Data Type & | ||
2. Non-Primitive Data Type | ||
Note : How Data is stored and How data is accsessed from the memory on the basis of this | ||
categorisation of data is done. | ||
Note : JavaScript is dynamically typed. | ||
---------------------------------------------------------------------------------------------------------- | ||
1. Primitive Data Type : 7 Types : String, Number, Boolean, Symbol, Null, undefined, BigInt. (These | ||
are call by value) */ | ||
|
||
const score = 100 | ||
const scoreValue = 100.3 | ||
// Both comes into number category there is noting like float value for decimal e.t.c like other languages. | ||
const isLoogedIn = true // Boolean | ||
const outsideTemp = null // Null | ||
let weather; // OR let weather = undefined // Undefined | ||
// Symbol, | ||
const id = Symbol('123') | ||
const anotherId = Symbol('123') | ||
/* The return type for both will be the data type (Symbol) and both id & anotherId treated diffrently. | ||
To check, */ | ||
console.log(id === anotherId); // false | ||
const bigNumber = 23873847283748289483n // By writing n at the end it gets converted into BigInt. | ||
|
||
|
||
/* ------------------------------------------------------------------------------------------------------- | ||
2. Reference Type (Non-Primitive) : array, objects, functions. */ | ||
|
||
// Array, | ||
const heros = ["ironman", "spiderman", "batman"]; | ||
// Objects, | ||
let myObj = { // We can store them into a Variable | ||
name: "Ayush", | ||
age: 21, | ||
} // The code inside these curly braces is object. | ||
// Function, | ||
const myFunction = function (){ // Defintion stored into a variable | ||
console.log("Hello World"); | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------------ | ||
Checking Data Types of all, | ||
Primitive, */ | ||
console.log(typeof scoreValue); // number | ||
console.log(typeof isLoogedIn); // boolean | ||
console.log(typeof outsideTemp); // object | ||
console.log(typeof weather); // undefined | ||
console.log(typeof anotherId); // symbol | ||
console.log(typeof bigNumber); // bigint | ||
|
||
// No Primitive, | ||
console.log(typeof heros); // Object | ||
console.log(typeof myObj); // object | ||
console.log(typeof myFunction); // function | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 6. Stack & Heap | ||
|
||
// * Some notes also available in note book with Diagram. Photo available in readme.md | ||
|
||
// Example of Stack, | ||
let myYoutubeName = "ayushyadavz" // Primitive Type so myYoutubeName will go into stack. | ||
let anotherName = myYoutubeName // This will have the copy value of myYoutubeName. | ||
anotherName = "amanyadavz" // It's value will go in stack. | ||
console.log(myYoutubeName); // Output : ayushyadavz (Original value did not change) | ||
console.log(anotherName); // Output : amanyadavz (Only Copy value is changed) | ||
/* So, here we have changed the copy not the original value. | ||
---------------------------------------------------------------------------------------- */ | ||
|
||
// Example of Heap, | ||
let userOne = { // UserOne will go into Stack. (Primitive) | ||
email: "user@google.com", | ||
upi: "user@ybl" | ||
} // items between {} will go into Heap. (Non-Primitive) | ||
let userTwo = userOne // userTwo will get reference from original value. | ||
userTwo.email = "ayush@google.com" | ||
console.log(userOne.email); // Output : ayush@google.com | ||
console.log(userTwo.email); // Output : ayush@google.com | ||
// (Original Value gets changed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* 7. Strings | ||
(i) Concatenation in Strings, */ | ||
const name = "Ayush" | ||
const repoCount = 19 | ||
// Outdated Syntax, | ||
console.log(name + repoCount + " etc."); // Output : Ayush19 etc. | ||
// Modern day Syntax, | ||
console.log(`My name is ${name} and I have total ${repoCount} repositories on GiHub.`); | ||
// Output : My name is Ayush and I have total 19 repositories on GiHub. | ||
/* Note: Benifits of this syntax is that we can use methods like, ${name.toUpperCase} e.t.c. | ||
-------------------------------------------------------------------------------------------------- */ | ||
|
||
// (ii) One more way of declaring strings, | ||
const gameName = new String('SuperMario') /* When we run this in Browser's console we can see | ||
some interesting things there. | ||
Some Methods of String, */ | ||
// (a) Accessing 0th key of gameName, | ||
console.log(gameName[0]); // Output : S | ||
// (b) Accessing prototype, | ||
console.log(gameName.__proto__); // Output : {} | ||
|
||
// (c) Accessing methods of Prototype, | ||
console.log(gameName.length); // Output : 10 | ||
console.log(gameName.toUpperCase()); // Output : SUPERMARIO | ||
// This will not change our original string cause as we know about Stack. | ||
console.log(gameName.charAt(3)); // Output : e | ||
console.log(gameName.indexOf('M')); // Output : 5 | ||
|
||
const discordName = "hitesh-hc" | ||
console.log(discordName.length); // Output : 9 | ||
const newString = discordName.substring(0, 4) // It will not include the 4th one. | ||
console.log(newString); // Output : hite | ||
|
||
const anotherString = discordName.slice(-8, 4) | ||
console.log(anotherString); /* Output : ite | ||
Note : We cannot give negative values in substring but we can give negative value in slice. */ | ||
|
||
const oneNewString = " hitesh " | ||
console.log(oneNewString.trim()) // Output : hitesh (will remove that spaces around the string.) | ||
|
||
const url = "https://ayush.com/ayush%20yadav" | ||
console.log(url.replace('%20', '-')); /* Output : https://ayush.com/ayush-yadav | ||
Also, */ | ||
console.log(url.includes('ayush')) // Output : true | ||
|
||
console.log(discordName.split('-')); /* Output : [ 'hitesh', 'hc' ] (Converted into array with | ||
two strings) */ | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* 8. Numbers And Maths | ||
(i) Numbers, | ||
When Javascript automatically defines the data type, */ | ||
const balance = 400 | ||
// console.log(balance); // Output: 400 | ||
// Note: Here Javascript automatically detected that the score type is number. | ||
|
||
// But we can also define explicitely the data type, | ||
const newBalance = new Number(400) | ||
// console.log(newBalance); // Output: [Number: 400] | ||
// Note: When we do this in browser console the we can see the methods. | ||
|
||
// --------------------------------------------------------------------------------------------------------- | ||
|
||
// Some Methods, | ||
// console.log(newBalance.toString()); // Output: 400 | ||
// Note: When we will check it's typeof the we will see that it is changed into strings. | ||
// console.log(newBalance.toString().length); // Output: 3 (cause 400 has 3 chracters.) | ||
|
||
// console.log(newBalance.toFixed(2)); // Output: 400.00 | ||
|
||
const otherNumbers = 123.8966 | ||
// console.log(otherNumbers.toPrecision(3)); // Output: 124 | ||
const otherNum = 23.8966 | ||
// console.log(otherNum.toPrecision(3)); // Output: 23.9 | ||
// Note: It means we want precise value but on how many values we want to focus. | ||
|
||
const hundreds = 1000000 | ||
// console.log(hundreds.toLocaleString('en-IN')); // Output: 10,00,000 (converted accor. to IN standards.) | ||
|
||
/* *******************************************MATHS******************************************************* | ||
(ii) Maths, */ | ||
console.log(Math); /* Output: Object [Math] {} | ||
Note: When we run this console.log(Math) in browser's console we will get many value inside object. */ | ||
|
||
console.log(Math.abs(-4)); // Output: 4 (Changes only minus values in plus) | ||
|
||
console.log(Math.round(4.6)); // Output: 5 | ||
console.log(Math.round(4.4)); // Output: 4 | ||
console.log(Math.ceil(4.2)); // Output: 5 (Choose top value) | ||
console.log(Math.floor(4.9)); // Output: 4 (Choose lowest value) | ||
|
||
console.log(Math.min(3, 2, 4, 6)); /* Output: 2 (Tells min value) | ||
Note: There is also "max". */ | ||
|
||
console.log(Math.random()); // Output: 0.36675387800250636 (Gives random numbers between 0 & 1) | ||
console.log((Math.random()*10) + 1); // Now numbers will range from 1 to 9 | ||
console.log(Math.floor(Math.random()*10) + 1); // Round offs the Random numbers at lowest value. | ||
|
||
const min = 10 | ||
const max = 20 | ||
console.log(Math.floor(Math.random() * (max - min + 1)) + min); | ||
// Note: (max - min) to get a range, (+ 1) to avoid zero, (+ min) to get minimum value to this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* 9. Dates | ||
Note: Our declared Dates has been calculated from 1 January 1970. | ||
Note: Dates has been calculated usually in Milliseconds. */ | ||
|
||
let myDate = new Date(); | ||
console.log(myDate); /* Output: 2024-04-24T08:15:11.671Z | ||
-------------------------------------------------------------------------------------- | ||
Conversion of Dates in readable form, | ||
Some Methods, */ | ||
console.log(myDate.toString()); // Output: Wed Apr 24 2024 13:47:36 GMT+0530 (India Standard Time) | ||
console.log(myDate.toDateString()); // Output: Wed Apr 24 2024 | ||
console.log(myDate.toLocaleString()); // Output: 24/4/2024, 1:49:14 pm | ||
console.log(myDate.toJSON()); // Output: 2024-04-24T08:20:04.530Z | ||
console.log(myDate.toISOString()); // Output: 2024-04-24T08:21:49.915Z | ||
console.log(myDate.toLocaleDateString()); /* Output: 24/4/2024 | ||
-------------------------------------------------------------------------------------- | ||
Typeof Date, */ | ||
console.log(typeof myDate); /* Output: Object | ||
-------------------------------------------------------------------------------------- | ||
Declaring a new or a specific date, */ | ||
// let myCreatedDate = new Date(2002, 10, 24) // Months starts with 0 in JS. | ||
// console.log(myCreatedDate.toDateString()); // Output: Sun Nov 24 2002 | ||
// let myCreatedDate = new Date(2002, 10, 24, 5, 3) | ||
// console.log(myCreatedDate.toLocaleString()); // Output: 24/11/2002, 5:03:00 am | ||
let myCreatedDate = new Date("11-24-2002") | ||
console.log(myCreatedDate.toLocaleString()); /* Output: 24/11/2002, 5:30:00 am | ||
-------------------------------------------------------------------------------------- */ | ||
|
||
let myTimeStamp = Date.now() | ||
console.log(myTimeStamp); // Output: 1713948664801 (Millisecond value from 1 Jan 1970 to current) | ||
console.log(myCreatedDate.getTime()); /* Output: 1038076200000 | ||
So now we can easily compare 1713948664801 and 1038076200000. | ||
Converting it into seconds, */ | ||
console.log(Math.floor(Date.now()/1000)); // Output: 1713950199 | ||
|
||
let myDates = new Date() | ||
console.log(myDates); | ||
console.log(myDates.getDay()); // Output: 3 | ||
console.log(myDates.getMonth() + 1); /* Output: 4 | ||
---------------------------------------------------------------------------------------- | ||
Some other syntax, */ | ||
`${myDates.getDay()} and the time is` | ||
// We can completely define what we want, | ||
myDates.toLocaleString('default', { | ||
weekday: "long" | ||
}) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* 1. Arrays | ||
(i) Declaring Array, */ | ||
const myArr = [0, 1, 2, 3, 4] | ||
// console.log(myArr[2]); // Output: 2 | Accsessing value of an Array. | ||
// Note: Array indexing starts from Zero. | ||
const heros = ["Thor", "IronMan", "SpiderMan"] | ||
// console.log(heros); // Output: [ 'Thor', 'IronMan', 'SpiderMan' ] | ||
const myArr2 = new Array(1, 2, 3, 4) | ||
// console.log(myArr2); // Output: [ 1, 2, 3, 4 ] | ||
/* Note: Run const numbers = [1, 2, 3, 4] in browser's console to see prototype access in Arrays. | ||
----------------------------------------------------------------------------------------------------- | ||
(ii) Array Methods, */ | ||
myArr.push(6) | ||
// console.log(myArr); // Output: [ 0, 1, 2, 3, 4, 6 ] | Pushes element in the end of the array. | ||
myArr.pop() | ||
// console.log(myArr); // Output: [ 0, 1, 2, 3, 4 ] | Removes element from the end of the array. | ||
myArr.unshift(9) | ||
// console.log(myArr); // Output: [ 9, 0, 1, 2, 3, 4 ] | Adds element to the starting of the array. | ||
myArr.shift() | ||
// console.log(myArr); // Output: [ 0, 1, 2, 3, 4 ] | Removes element from the starting of the array. | ||
|
||
// console.log(myArr.includes(9)); // Output: false | ||
|
||
// console.log(myArr.indexOf(4)); // Output: 4 | ||
// Note: Indexing starts from Zero. | ||
// console.log(myArr.indexOf(9)); // Output: -1 | ||
// Asking any value which doesn't exist in index gives you -1. | ||
|
||
const newArr = myArr.join() | ||
// console.log(newArr); // Output: 0,1,2,3,4 | ||
// console.log(typeof newArr); // Output: string | ||
/* Note: .join binds and converts array into strings. | ||
----------------------------------------------------------------------------------------------------- | ||
(iii) slice, splice (Methods) */ | ||
console.log("A ", myArr); // Output: A [ 0, 1, 2, 3, 4 ] | ||
const myNewArr1 = myArr.slice(1, 3) /* Include value at index 1 and 2 but don't | ||
include value at index 3. */ | ||
console.log(myNewArr1); // Output: [ 1, 2 ] | ||
console.log("B ", myArr); // Output: B [ 0, 1, 2, 3, 4 ] | ||
// Note: As we can see using slice doesn't affect original array. | ||
|
||
const myNewArr2 = myArr.splice(1, 3) | ||
console.log(myNewArr2); // Output: [ 1, 2, 3 ] | ||
console.log("C ", myArr); // Output: C [ 0, 4 ] | ||
// Note: But in splice we can see it removes the elements and manipulates the original array. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 2. Arrays | ||
|
||
// (i) Methods to merge Arrays, | ||
const marvel_heros = ["Thor", "Hulk", "Loki"] | ||
const dc_heros = ["Flash", "Superman", "Batman"] | ||
// marvel_heros.push(dc_heros) | ||
console.log(marvel_heros); // Output: [ 'Thor', 'Hulk', 'Loki', [ 'Flash', 'Superman', 'Batman' ] ] | ||
/* Note: In this dc_heros array has been taken as an 4th element in marvel_heros array. | ||
And this is not a good practice or a good syntax. */ | ||
|
||
const all_heros = marvel_heros.concat(dc_heros) | ||
console.log(all_heros); // Output: [ 'Thor', 'Hulk', 'Loki', 'Flash', 'Superman', 'Batman' ] | ||
// Note: .push pushes on existing array & .concat returns a new array. | ||
|
||
const all_new_heros = [...marvel_heros, ...dc_heros] | ||
console.log(all_new_heros); // Output: [ 'Thor', 'Hulk', 'Loki', 'Flash', 'Superman', 'Batman' ] | ||
/* Note: This methods is used more, cause we can add multiple values in this ([..., ..., ...]). | ||
----------------------------------------------------------------------------------------------------*/ | ||
|
||
const another_array = [1, 2, 3, [4, 5], 3, 4, [9, [4, 3]]] | ||
const another_new_array = another_array.flat(Infinity) | ||
console.log(another_new_array); // Output: [1, 2, 3, 4, 5, 3, 4, 9, 4, 3] | ||
/* Note: All the values spreaded into a Array. | ||
---------------------------------------------------------------------------------------------------- | ||
(ii) Converting into Array, */ | ||
console.log(Array.isArray("Ayush")); // Output: false | Ayush is not an Array. | ||
// Converting it into an Array, | ||
console.log(Array.from("Ayush")); // Output: [ 'A', 'y', 'u', 's', 'h' ] | Converted string into Array. | ||
// Objects into Array, | ||
console.log(Array.from({name: "Ayush"})); // Output: [] | Returns an empty array. | ||
|
||
const score1 = 100 | ||
const score2 = 200 | ||
const score3 = 300 | ||
console.log(Array.of(score1, score2, score3)); // Output: [ 100, 200, 300 ] |
Oops, something went wrong.