Skip to content

Commit e82bf66

Browse files
refactor: datatype
1 parent 2b62077 commit e82bf66

22 files changed

+389
-181
lines changed

11_array/array_method/7_join.js

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
// join()
22

33
// What
4-
// - It return new string that contain all the elements of array in string format which separated by comma
4+
// - The join() method in JavaScript is used to concatenate all elements of an array into a single string, separated by a specified delimiter.
55

6-
const myArray = [1, 2, 3, 4, 5];
7-
const myArray2 = myArray.join();
6+
// Why
7+
// - The join() method is useful when you need to convert an array of elements into a formatted string for display, storage, or transmission.
88

9-
console.log(myArray2);
10-
console.log(typeof myArray2);
9+
// How
10+
// - The join() method takes an optional delimiter as an argument and returns a new string.
11+
// - If no delimiter is provided, the elements of the array are concatenated without any separation.
12+
// - If a delimiter is provided, the elements of the array are concatenated with the delimiter between each pair of elements.
13+
14+
// Syntax
15+
// array.join("delimiter");
16+
17+
// Example
18+
// Joining an array of strings into a single string
19+
const fruits = ["apple", "banana", "orange"];
20+
const fruitString = fruits.join(", ");
21+
console.log(fruitString); // Output: "apple, banana, orange"
22+
23+
// Joining an array of numbers into a single string
24+
const numbers = [1, 2, 3, 4, 5];
25+
const numberString = numbers.join("-");
26+
console.log(numberString); // Output: "1-2-3-4-5"
27+
28+
// Joining an array of objects into a single string
29+
const people = [
30+
{ name: "John", age: 25 },
31+
{ name: "Jane", age: 30 },
32+
{ name: "Bob", age: 35 },
33+
];
34+
const personString = people
35+
.map((person) => `${person.name} (${person.age})`)
36+
.join("; ");
37+
console.log(personString); // Output: "John (25); Jane (30); Bob (35)"

11_array/array_method/reverse.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// reverse()
2+
3+
// What
4+
// - The reverse() method in JavaScript is used to reverse the order of elements in an array.
5+
6+
// Why
7+
// - The reverse() method is useful when you need to manipulate or process an array by reversing its order.
8+
// - It is commonly used in various programming scenarios, such as sorting data in descending order, reversing a sequence of elements, or creating user-friendly interfaces.
9+
10+
// How
11+
// - The reverse() method modifies the original array in-place and does not create a new array.
12+
// - It reverses the order of elements in the array by swapping the first element with the last element, the second element with the second-to-last element, and so on.
13+
14+
// Syntax
15+
// array.reverse()
16+
17+
// Example
18+
// Reversing an array of numbers
19+
const numbers = [1, 2, 3, 4, 5];
20+
numbers.reverse();
21+
console.log(numbers); // Output: [5, 4, 3, 2, 1]
22+
23+
// Reversing an array of strings
24+
const fruits = ["apple", "banana", "orange"];
25+
fruits.reverse();
26+
console.log(fruits); // Output: ["orange", "banana", "apple"]
27+
28+
// Reversing an array of objects
29+
const people = [
30+
{ name: "John", age: 25 },
31+
{ name: "Jane", age: 30 },
32+
{ name: "Bob", age: 35 },
33+
];
34+
people.reverse();
35+
console.log(people);
36+
// Output: [
37+
// { name: "Bob", age: 35 },
38+
// { name: "Jane", age: 30 },
39+
// { name: "John", age: 25 },
40+
// ]
41+
42+
// Note
43+
// - The reverse() method modifies the original array in-place. If you need to preserve the original order of elements, you should create a copy of the array before calling the reverse() method.
44+
// - The reverse() method does not return a new array. Instead, it modifies the existing array.

2_output/console_table.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
// console.table()
22

33
// What
4-
// - This method is same as a console.log() but it will accept variable as a array and print their output in table format.
4+
// - console.table() is a method in JavaScript that displays tabular data in a visually appealing format in the browser's console.
5+
// - It takes an array or object as an argument and displays it as a table
6+
7+
// Why
8+
// - console.table() is useful for debugging and inspecting complex data structures, such as arrays of objects or nested objects.
9+
// - It provides a clear and organized view of the data, making it easier to analyze and understand.
10+
11+
// How
12+
// - console.table() iterates over the elements of the provided array or object and displays them in a table format in the console.
13+
// - It automatically generates column headers based on the properties of the elements.
514

615
// Syntax
7-
// console.table([variable, variable, variable]);
16+
// console.table(variable,array or Object);
817

18+
// Example: 1
919
const variable1 = 1;
1020
const variable2 = 2;
1121
const variable3 = 3;
1222

1323
console.table([variable1, variable2, variable3]);
24+
25+
// Example: 2
26+
const students = [
27+
{ name: "John", age: 20, grade: "A" },
28+
{ name: "Alice", age: 19, grade: "B" },
29+
{ name: "Bob", age: 21, grade: "C" },
30+
];
31+
32+
console.table(students);
33+
34+
// Note
35+
// - console.table() is a helpful debugging tool, but it should not be used for logging or displaying data in a production environment.
36+
37+
// When
38+
// - Use console.table() when you need to inspect complex data structures, such as arrays of objects or nested objects, in the browser's console.
39+
// - Use console.table() to display tabular data in a more readable format for debugging purposes.
40+
// - Use console.table() to analyze and compare data in a structured manner.

3_directive/use_strict.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Directive
22

3-
"use strict";
4-
53
// What
64
// - "Use strict" is a directive in JavaScript that activates strict mode, a stricter set of rules for interpreting and executing JavaScript code.
75

@@ -10,3 +8,24 @@
108

119
// How
1210
// - To activate strict mode, you include the string literal "use strict"; at the beginning of a script or a function. This directive tells the JavaScript engine to interpret the code that follows it in strict mode.
11+
12+
// Syntax
13+
"use strict";
14+
15+
// Example
16+
// Strict mode example
17+
function sum(a, b) {
18+
return a + b;
19+
}
20+
21+
console.log(sum(1, "2")); // This will throw a TypeError in strict mode
22+
23+
// Note
24+
// - The use strict directive should be placed at the beginning of a script or function to enable strict mode for that scope.
25+
// - Strict mode can help catch common coding errors and enforce best practices, making your code more reliable and secure.
26+
27+
// When
28+
// - Use the use strict directive to enable strict mode in your JavaScript code.
29+
// - Use strict mode to catch common coding errors and enforce best practices.
30+
// - Use strict mode when working on large projects or collaborating with others to ensure consistent code quality.
31+
// - Use strict mode when you want to write modern JavaScript code that leverages new features and best practices

4_variable/1_variable.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
// - `var` keyword has been deprecated so it's not recommended to use. Only use `let` and `const`.
1212

1313
// Syntax
14-
1514
// Single variable
1615
var variable = 0; // Not Recommended
1716
let variable1 = 1;
1817
const variable2 = 2;
1918

2019
// Multiple variable with same value
21-
// -First declare then initialize
20+
// - First declare then initialize
2221
let variable3, variable4, variable5;
2322
variable3 = variable4 = variable5 = 1;
2423

4_variable/2_var_keyword.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
// var Keyword
2-
3-
// Note
4-
// - `var` keyword has been deprecated so it's not recommended to use.
5-
// - It can be initialized later.
6-
// - It can be redeclare.
7-
// - It can be reassigned.
8-
// - It has global scope.
1+
// `var` Keyword (Not Recommended)
92

103
// Syntax
114
// var variableName = value;
@@ -20,4 +13,11 @@ var variable = "value";
2013

2114
// Reassigned
2215
var variable = "value1";
23-
variable = "value2";
16+
variable = "value2";
17+
18+
// Note
19+
// - It can be initialized later.
20+
// - It can be redeclare.
21+
// - It can be reassigned.
22+
// - It has global scope.
23+
// - The var keyword is not recommended for modern JavaScript development. It has been deprecated and replaced with let and const. Using var can lead to unexpected behavior and bugs in your code.

4_variable/3_let_keyword.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
// Let Keyword
2-
3-
// Note
4-
// - It can be initialized later.
5-
// - It can be reassigned.
6-
// - It can't be redeclare.
7-
// - It can be redeclare but in different scope.
8-
// - It has block scope
1+
// `let` Keyword
92

103
// Syntax
114
// let variableName = value;
@@ -28,3 +21,15 @@ let variable2 = "value"; // Declare in global scope
2821
function myFunction(parameter) {
2922
let variable2 = "value"; // Redeclare in function scope
3023
}
24+
25+
// Note
26+
// - It can be initialized later.
27+
// - It can be reassigned.
28+
// - It can't be re-declare.
29+
// - The let keyword is not hoisted, meaning it cannot be accessed before it is declared in the code.
30+
// - The let keyword allows you to declare variables with the same name within different blocks, as long as they are in different scopes.
31+
32+
// When
33+
// - Use let when you need to declare a variable with block scope.
34+
// - Use let when you need to declare a variable that can be reassigned and re-declared within the same block.
35+
// - Use let when you need to declare a variable that is only accessible within a specific block, such as inside a function or loop.

4_variable/4_const_keyword.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
// Const Keyword
1+
// `const` Keyword
22

33
// What
4-
// - The const keyword is use to create constant.
4+
// - The const keyword in JavaScript is used to declare a read-only variable.
55

6-
// When
7-
// - Always declare a variable with const when you know that the value should not be changed.
8-
// Use const when you declare:
9-
// A new Array
10-
// A new Object
11-
// A new Function
12-
// A new RegExp
6+
// Why
7+
//- The const keyword is used to declare variables in JavaScript to store and manipulate data. It provides immutability and helps prevent accidental modifications to variables.
8+
//- The const keyword is recommended for modern JavaScript development as it enforces read-only variables and helps prevent bugs.
139

14-
// Note
15-
// - It can only initialized when it's declared.
16-
// - It can't be reassigned.
17-
// - It can't be redeclare.
18-
// - It can be redeclare but in different scope.
10+
// How
11+
// - When you declare a variable using the const keyword, JavaScript reserves memory space to store the variable's value. You can then assign a value to the variable and use it in your program.
12+
// - The const keyword ensures that the variable's value cannot be modified once it is assigned. If you try to reassign a value to a const variable, JavaScript will throw an error.
1913

2014
// Syntax
2115
// const constantName = value;
@@ -40,13 +34,25 @@ function myFunction(parameter) {
4034

4135
// Constant Objects and Arrays
4236
// - The keyword const is a little misleading.
43-
// - It does not define a constant value. It defines a constant reference to a value.
37+
// - It does not define a constant value. It defines a constant reference to a value. Because of this...
4438

45-
// Because of this you can NOT:
39+
// Can't:
4640
// - Reassign a constant value
4741
// - Reassign a constant array
4842
// - Reassign a constant object
4943

50-
// But you CAN:
44+
// Can:
5145
// - Change the elements of constant array
52-
// - Change the properties of constant object
46+
// - Change the properties of constant object
47+
48+
// Note
49+
// - It can initialized & declared at same time.
50+
// - It can't be reassigned.
51+
// - It can't be redeclare.
52+
// - The const keyword is not hoisted, meaning it cannot be accessed before it is declared in the code.
53+
// - The const keyword allows you to declare variables with the same name within different scopes, as long as they are in different blocks.
54+
// - The const keyword is block-scoped, meaning that the variable's value can only be accessed within the block where it is declared.
55+
56+
// When
57+
// - Always declare a variable with const when you know that the value should not be changed.
58+
// - Use const when you declare a new array, object or function

4_variable/var_let_and_const.md

-12
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,3 @@
66
| - Can be reassign | - Can be reassign | - Can't be reassign |
77
| - Can be redeclare in same scope | - Can't be in same scope redeclare | - Can't be in same scope redeclare |
88
| - Can be redeclare with different scope | - Can be redeclare with different scope | - Can be redeclare with different scope |
9-
10-
# NOTE
11-
12-
- You can declare variable without using any of this keyword. But that is not recommend.
13-
14-
# When to Use var, let, or const?
15-
16-
1. Always declare variables
17-
2. Always use const if the value should not be changed
18-
3. Always use const if the type should not be changed (Arrays and Objects)
19-
4. Only use let if you can't use const
20-
5. Only use var if you MUST support old browsers.

5_datatype/1_datatype.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Type Of Datatype
2+
3+
// What
4+
// - Data types in JavaScript are the different types of values that can be stored and manipulated in a program.
5+
// - They define the kind of data that a variable can hold and the operations that can be performed on that data.
6+
7+
// Why
8+
// - Data types help ensure that your program behaves correctly and handles different types of data appropriately.
9+
// - They provide a foundation for writing robust and maintainable code.
10+
11+
// How
12+
// - JavaScript has a set of built-in data types that define the kind of values that can be stored and manipulated in a program. These data types include:
13+
// - Number: Represents numeric values, including integers and floating-point numbers.
14+
// - String: Represents textual data, enclosed in quotes.
15+
// - Boolean: Represents logical values, which can be either true or false.
16+
// - Object: Represents complex data structures, such as arrays, functions, and custom objects.
17+
// - Undefined: Represents a variable that has not been assigned a value.
18+
// - Null: Represents a variable that has been explicitly assigned a value of null.
19+
20+
// Undefined
21+
// - You can assign literally a value as a `undefined`
22+
let undefined1 = undefined;
23+
// - Declared variable which doesn't assigned any value yet also has undefined as a value.
24+
let undefined2;
25+
26+
// Null
27+
// - Null is standalone value. To understand this let say you are creating weather application which give you the temperature of selected city. For any reason server unable to send you the temperature that means there is no temperature value. At that case weather app shouldn't show `0` on screen. Because 0 also refer as temperature value. At that time we use null to show there is actually no temperature and server are unable to fetch data of temperature.
28+
// - WHEN CHECK THE TYPEOF NULL IT WILL SHOW IT IS AN OBJECT.
29+
let myNull = null;
30+
31+
// Non-Primitive Data Types (Reference, Complex)
32+
33+
// Object
34+
const object = {
35+
key1: "value1",
36+
key2: "value2",
37+
key3: "value3",
38+
};
39+
40+
// Array
41+
const array = [1, 2, 3, 4, 5];
42+
43+
// Function
44+
function myFunction(parameter) {
45+
return parameter;
46+
}
47+

0 commit comments

Comments
 (0)