Basic
Basic
js
1
2
3 /*
4 1. Primitive data types:
5 Copy by Value: When assigning a primitive type (like number,
string, boolean), a new copy of the value is created. Changes to the
copy do not affect the original value.
6 Ex: below
7 */
8 let myName = "Anurag Gupta"; console.log(myName);
9
10 let myName2 = myName ; console.log(myName2);
11
12 myName2 = "Lala gupta"; console.log(myName2);
13
14 console.log(myName); //see the actual value is not changed
15
16
17 /*
18 2. Non-primitive or By Reference:
19 Copy by Reference: When assigning a non-primitive type (like object,
array), a reference to the same memory location is used. Changes to the
reference affect the original object.
20 Ex : given below
21 */
22
23
24 let obj1 = {myName : "Anurag",age : 20, yearOfClg : "3rd"}
25 console.log(obj1);
26
27 let obj2 = obj1
28 console.log(obj2);
29
30 obj2.myName = "lala"; console.log(obj2);
31 console.log(obj1); //assign the value in obj2 also reflected in obj1
32
33 obj1.age = 21; console.log(obj1);
34 console.log(obj2);
02.1_imp_ use strict mode in js\strict.js
1 /*
2 "use strict"; is a directive that activates strict mode in JavaScript
or newer version of js, enforcing stricter rules for better code
quality.
3
4 Benefits
5 Catches Errors: Throws errors for common mistakes.
6 Disables Unsafe Features: Prevents use of certain features that are
error-prone.
7 */
8
9 // Non-strict mode (No "use strict";)
10 x = 10; // No error, `x` is created as a global variable
11 console.log(x); // 10
12
13
14 // Strict mode (with "use strict";)
15 "use strict";
16
17 try {
18 x = 10; // Error: `x` is not declared, will throw a ReferenceError
19 } catch (e) {
20 console.error(e); // ReferenceError: x is not defined
21 }
22
23 // Correct way to declare a variable in strict mode
24 "use strict";
25 let x = 10; // No error, `x` is properly declared using `let`
26 console.log(x); // 10
03_ diiferent types of operations\opration.js
1
2 // In JavaScript, a string is a sequence of characters used to
represent text, enclosed in single quotes ('), double quotes ("), or
backticks (`) for template literals. Strings are immutable and can be
manipulated using various built-in methods
3
4 let myName = "Anurag"
5 let age = 20;
6
7 // traditional method to print the string
8 console.log("My name is " + myName + " and age is " + age + " ok");
//no recommed to use this way
9
10 // modern way to print string and known as string interpolation
11 console.log(`my name is ${myName} and age is ${age} ok`); //recomended
to use this way
12
13 // another way to initialize the string using String class
14 let obj = new String("Anurag");
15
16 console.log(obj, obj.length , obj[0])
17
05_strings concept\strings_methods.js
1 // date
2 let date = new Date()
3
4 console.log(date);
5 console.log(date.toString());
6 console.log(date.toISOString());
7 console.log(date.toUTCString());
8 console.log(date.toLocaleTimeString());
9 console.log(date.getFullYear());
10 console.log(date.getMonth());
11 console.log(date.getDate());
12 console.log(date.getDay());
13 console.log(date.getHours());
14
15
16 const newDate = new Date(2023,8,5)
17 console.log(newDate.toString());
18
19 // maths and math random number
20
21
22 console.log(Math);
23
24 // mostly used is random() always betwwen 0 to 1j
25 console.log(Math.random());
26
27
28 // 1. Rounds a number up to the nearest integer
29 console.log(Math.ceil(4.5));
30
31 // 2. Rounds a number down to the nearest integer
32 console.log(Math.floor(4.5));
33
34 // 3. Rounds a number to the nearest integer; 4.4 to 4, 4.5 to 5
35 console.log(Math.round(4.4), Math.round(4.5));
36
37 // 4. Returns the largest number from a set of values
38 console.log(Math.max(4, 5, 8, 6, 7, 10));
39
40 // 5. Returns the smallest number from a set of values
41 console.log(Math.min(4, 5, 8, 6, 7, 10));
42
43 // 6. Returns the absolute value (magnitude) of a number
44 console.log(Math.abs(-10), Math.abs(10));
45
46 // 7. The value of π (pi), approximately 3.14159
47 console.log(Math.PI);
48
49 // 8. Returns the square root of a number
50 console.log(Math.sqrt(81));
51
52 // mainly we use this random method to guess the number between max or
min number like 10 or 20 ke bich me choose kro
53
54 // trick to do as we know radom gives only val between 0 to 1
55
56 let minNum = 10;
57 let maxNum = 20
58
59 let generated_num_logic = Math.random() * (maxNum - minNum + 1) +
minNum; console.log(generated_num_logic);
60
61 let actual_generated_num= Math.floor(generated_num_logic)
62 console.log(actual_generated_num
);
63
64
65 // number in js
66
67 // dynamic declaration
68 let score = 787;
69 console.log(score , typeof score);
70
71
72 // but we can mannualy or hardcore the number through number class
73
74 let score2 = new Number(1234)
75 console.log(score2 , typeof score2);
76
77
78 // some Number class methods
79
80 let numberVal = 454.5685
81
82 console.log(numberVal.toFixed(2));
83
07_Arrays and its methods\1_Array.js
1 /* Object:
2 A collection of key-value pairs where each key (also called a
property) is a string, and each value can be any data type, including
other objects or functions.
3
4 **TWO WAYS TO REPERSNT OBJECTS:**
5 1. Singleton object:
6 - it created using Object constructor
7
8 2. Object Literal:
9 - A simple way to create objects using curly braces {} with
key-value pairs.
10
11 */
12
13 const rollNo = Symbol('rollNo'); // Create a unique symbol
14
15 let obj = {
16 myName : "Anurag", //behind the scene the key also get converted
in this "myName" i.e. in double cotts
17 age : 20,
18 colorTone : "kaala hai",
19 MyFunc : function(){console.log("calling from the object");},
20 // here how to use symbol as a property key in obj
21 [rollNo] : 17,
22 // how to use object key as variable using 'this' keyword
23 greetFunc : function(){console.log(`namaste ${this.myName}`);}
//here this refer to obj i.e. object
24 }
25
26 console.log(obj);
27 // console.table(obj)
28
29 // accessing the value using dot
30 console.log(obj.age,obj.colorTone,typeof obj);
31
32 //another way to acces the objec value using keys with square double
quotes
33 console.log(obj["age"],obj["colorTone"],typeof obj);
34
35 console.log(obj[rollNo], typeof rollNo);
36 obj.MyFunc()
37 obj.greetFunc()
38
39 // HOW TO CHANGE THE VALUE OF KEYS
40
41 obj.myName = "Anurag Gupta"
42 console.log(obj);
43
44
45
08_Objects and its methods\2_objects_Methods.js
1
2 // creating object using Object class
3 let insta_obj = new Object()
4 console.log(insta_obj);
5
6 // Adding elements in the object
7
8 insta_obj.id = 111533
9 insta_obj.user_name = "anurag_2004"
10 insta_obj.isLoggedIn = true
11 insta_obj.isStatusUpdate = false
12
13 console.log(insta_obj);
14
15 //SOME object class METHODS OR OPERATIONS
16
17 console.log(Object.keys(insta_obj));// Listing all property or keys
names
18 console.log(Object.values(insta_obj));// Listing all property values
19 console.log(Object.entries(insta_obj));// Listing all key-value pairs
in an array form
20
21
22 let person1 = {
23 ogName : "Anurag",
24 RollNo : 17
25 }
26
27 let person2 = {nickname : "lala",age : 20}
28
29 // combine the two or more object using Object class assign method in
an single obj
30 let combindePersonDet = Object.assign(person1,person2)
31 console.log(combindePersonDet);
32
33 console.log(person1.hasOwnProperty("RollNo"));
34
35
36 // another general way to combine the 2 or more object using spread
oprator
37 let combindePersonDet_sp
eradOperator = {...person1,...person2}
38 console.log(combindePersonDet_sp
eradOperator);
39
40
08_Objects and its methods\3_object_destructing.js
1
2
3 let obj = {myName : "Anurag",rollNo:17,isInClg : false}
4 console.log(obj);
5
6
7 // accessing one key's value from an objec
8 console.log(obj.myName,obj.rollNo);
9
10 // another way to accessing the key's value using object destructuring
11
12 let {myName,rollNo} = obj;
13 console.log(myName,rollNo);
14
15 // we can give a shortcut name for accessing the key like alias means
short form like "i dont know" : idk(shortcut or alias)
16 let {myName:nam , rollNo : id} = obj
17 console.log(nam , id);
18
19
20
09_function and Types of function\1_function.js
1 /*
2 A function in JavaScript is a reusable piece of code that performs
a specific task or calculates a value. You define a function once and
can call or invoke it multiple times throughout your code.
3
4 syntax :
5 function function_name(){
6 // code
7 }
8
9 🔴 Use 'return' when you need to send a result from a function
and use that result elsewhere in your code.
10 Use 'console.log' to output information to the console for
debugging or checking values. 🔴
11 */
12
13 // keyword func_name ()indicate func {}scope of function
14 function greet () {
15 console.log("My");
16 console.log("Name");
17 console.log("is");
18 console.log("Anurag");
19 };
20 greet()
21
22 // some basic types of function
23
24 // anymous function--> useed in object example
25 let obj= {
26 myName : "Anurag",
27 namaste : function(){console.log("Namaste",this.myName); }
28 };
29 obj.namaste();
30
31 // arrow function : explicit means here we use the return
32 const func_name2=()=>{return `I am arrow function`}
33 console.log(func_name2());
34
35 // Arrow function : Implicit means here we not use return statment
36 const func_name2_I = ()=> `i am arrow function`;
37 console.log(func_name2_I());
38
39
40
41
42 // IIFE (Immediately Invoked Function Expression:
43 // A function that is defined and executed immediately.
It is used to create a new scope and prevent variable leaks into the
global scope
44
45 (function() {
46 // code to execute immediately
47 console.log('I am an IIFE!');
48 })();
49
50 // parameter in IIFE
51
52 ((name)=>{console.log(`my name is ${name}`);})("Lala");
53
54 /*
55 create process
56 (in firt braces function will declare)(); //AND SET YOUR IIFE DONE😀
57 */
09_function and Types of function\2_more_types_basedOnParameter.js
1 /*
2 syntax:
3 **declaring function**
4 function func_name (parameter){
5 \\codes or logic
6 };
7
8 **calling the function**
9 func_name(argument);
10
11 📝
12 Parameter: Function ke definition mein likha gaya variable.
13 Example: function add(x, y) {} (x aur y parameters hain)
14
15 Argument: Function call karte waqt di gayi actual value.
16 Example: add(2, 3); (2 aur 3 arguments hain)
17 📝
18
19 */
20
21
22 function addNum(num1,num2){
23 console.log(num1 + num2);
24 }
25
26 addNum(45,10);
27 // Type Coercion in JavaScript is the automatic conversion of values
from one type to another Data type, such as converting a number to a
string or vice versa, to perform operations like addition.
28 addNum(45,"40");addNum("a",65)
29
30
31 // Default parameter - ye hum tab use krte hai jab hume parametr ki
value empty or undefined nai chaiye hoti hai
32
33 function greet(name="Dear"){
34 console.log(`Hello ${name}😊`);
35 }
36 greet()
37
38 // handle the parameter in this case we dont want any default value we
need actual argument, otherwise send msg enter the name or custom error
msg so let see
39
40 function greet2(name){
41 if(name===undefined){
42 // console.log("mai return ke uper likha hu to mai chalunga");
43 return `plz pass ur name in greet2`
44 }
45 else{
46 // console.log("mai return ke uper likha hu to mai chalunga");
47 return `Hello ${name}`
48 console.log("always remeber reurn ke bad ka koi code run nai
hota hai");
49 }
50 }
51
52 console.log(greet2());
53 console.log(greet2("Anurag"));
54
55
56 /*
57 Rest Operator (...): Used in function parameters to collect multiple
arguments into an array
58 Spread Operator (...): Used to expand an array or object into
individual elements.(we already familiar with this spread opertot)
59 **************** BOTH ARE SAME BUT USE CASES ARE DIFFERENT ONLY
**************************
60
61 Kabhi kabhi humare pass aisi conditon aa jati hai jaha pr
hum exact kitne parameter pass honge function pta nai hota to aise
62 situations ko handle krne ke liye hum spread operator[...]
63
64 jaise grocery shop hai wah per fruits hai to hume exact nai
pta customer kitne fruits lega
65
66 */
67
68 function allFruits(...nameOfFruits){
69 console.log(`Name of fruits : ${nameOfFruits}`);
70 console.log(`Total Fruits : ${nameOfFruits.length}`);
71
72 }
73
74 allFruits("apple","kele","Aaam😋","orange")
75
09_function and Types of
function\3_how_to_use_object_and_array_in_function.js
1
2
3 // 1. USING OBJECT IN FUNCTION
4 const personObj = {
5 name: "Anurag",
6 age: 20
7 };
8
9 //pta hai function ka nam odd but understanding
10 function use_of_obj_in_func(anyObject) {
11 console.log(`my name is ${anyObject.name} and age is
${anyObject.age} years old.`);
12 console.log(anyObject);
13 }
14
15 use_of_obj_in_func(personObj)
16
17 // also we can pass direct object {} in argument
18 use_of_obj_in_func({name:"bacha hu",age:4,newAdd : "bus dekhne ke liye
add kiya hu 😁"})
19
20
21
22 // 2. USING ARRAY IN FUNCTION
23
24 function use_of_arr_in_func(anyArray){
25 console.log(anyArray);
26 console.log(`printing the 1st value (i.e. index[0]) of array :
${anyArray[0]}`);
27 console.log(`Length of given array is ${anyArray.length}`);
28 }
29
30 let fruitsArr = ["Apple","orange","banana","cherry"]
31
32 use_of_arr_in_func(fruitsArr);
33
34 // also we can array manually in function
35 use_of_arr_in_func([17,445,987,54,35,10]);
09_function and Types of function\4_Implicit_and_Explicit_return.js
1
2 /*
3 1. Block Body Arrow Function: [also know as Explicit return]
4 Terminology: "Block body" or "Block syntax"
5 Description: Uses curly braces {} and requires an explicit return
statement.
6 */
7
8 let block_body = () => {return `kuch nai`};
9 console.log(block_body());
10
11 /*
12 2. Concise Body Arrow Function: [also know as Implicit return]
13 Terminology: "Concise body" or "Expression body"
14 Description: Directly returns the value without curly braces or
return keyword. but you can use braces for better readablity '()'
15 */
16
17 // let concise_body = () => `kuch nai`;
18 let concise_body = () => (`kuch nai`);
19 console.log(concise_body());
20
21 // this is use in mostly higher order functions like map, filter and
reduce