Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74866c8

Browse files
committed
Data Types DONE - Need to update later after basics
1 parent a3bef52 commit 74866c8

File tree

2 files changed

+284
-0
lines changed

2 files changed

+284
-0
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

‎03-Data-Types.md

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
**Table of Content**
2+
3+
- [Data Types in JavaScript](#data-types-in-javascript)
4+
- [Numeric Data Type](#numeric-data-type)
5+
- [String Data Type](#string-data-type)
6+
- [Boolean Data Type](#boolean-data-type)
7+
- [Null Data Type](#null-data-type)
8+
- [Undefined Data Type](#undefined-data-type)
9+
- [Symbols Data Type](#symbols-data-type)
10+
- [Objects in JavaScript](#objects-in-javascript)
11+
- [Create, Read, Update and Delete (CRUD) in Objects](#create-read-update-and-delete-crud-in-objects)
12+
- [Create](#create)
13+
- [Read](#read)
14+
- [Update](#update)
15+
- [Delete](#delete)
16+
- [Other Useful Methods to Know](#other-useful-methods-to-know)
17+
- [Built-In JavaScript Objects](#built-in-javascript-objects)
18+
- [Array Object](#array-object)
19+
- [Properties of an Array](#properties-of-an-array)
20+
21+
# Data Types in JavaScript
22+
23+
There are six types of primitive data types we use in JavaScript.
24+
25+
- numeric
26+
- string
27+
- Boolean
28+
- null
29+
- undefined
30+
- symbol (introduced in ES6)
31+
32+
## Numeric Data Type
33+
34+
Numeric data type handles numbers. It can be any numbers including decimal numbers like 3.14.
35+
36+
```js
37+
let number = 1;
38+
let integer = 1.12345;
39+
let negativeNumber = -1;
40+
let negativeInteger = -1.1234;
41+
```
42+
43+
## String Data Type
44+
45+
String data type holds letters and symbols. We wrap our string of texts in quotation marks. We can use either of single('') or double quotation ("") mark. But we need to make sure we start and end with the same quotation. in some case, if we want to include the quotation in our string of texts, we can escape the string quotes by placing a backslash infornt of them.
46+
47+
```js
48+
let quote = "Everybody has a monster";
49+
let mixedQuote = 'This keeps the "Quotation Mark".';
50+
```
51+
52+
## Boolean Data Type
53+
54+
Boolean data type holds the value either true or false. We can store _true/false_ in a variable by typing them without quotation.
55+
56+
```js
57+
var isSingle = true;
58+
```
59+
60+
## Null Data Type
61+
62+
When we want a variable to be empty but not undefined , we set the variable to null (without quotation mark).
63+
64+
```js
65+
let salary = null;
66+
```
67+
68+
## Undefined Data Type
69+
70+
JavaScript is a weakly typed language, therefore the data type for variable are determined only after some value is stored in the variable. So, if no value is stored in a varibale, it is initalized as undefined.
71+
72+
## Symbols Data Type
73+
74+
Symbols data types are the primitive data types introsuces in ECMAScript 2015. Once we create a symbol, its value are kept private and for internal use. All that remains is the symbol reference.
75+
76+
We can create a symbol by simply calling Symbol() function.
77+
78+
```js
79+
const myVariable = Symbol();
80+
```
81+
82+
We get a new and unique symbol everytime we invoke a Symbol function(). Each symbol is guaranteed to be different from other symbols created.
83+
84+
```js
85+
Symbol() === Symbol(); //OUTPUT: false
86+
```
87+
88+
We can also pass parameter to Symbol() function which can be useful for debugging.
89+
90+
```js
91+
console.log(Symbol("Some Test"));
92+
```
93+
94+
We mostly use Symbols to avoid name clashing between object properties sicnce no symbol is equal to other.
95+
96+
```js
97+
const NAME = Symbol();
98+
const person = {
99+
[NAME]: "Flavio",
100+
};
101+
102+
person[NAME]; //'Flavio'
103+
104+
const RUN = Symbol();
105+
person[RUN] = () => "Person is running";
106+
console.log(person[RUN]());
107+
```
108+
109+
**typeof operator**
110+
111+
If we ever want to know the data type of a certain variable, we can use **typeof** operator.
112+
113+
```js
114+
let name = "Prabesh";
115+
console.log(typeof name); //OUTPUT: String
116+
117+
let isSingle = true;
118+
console.log(typeof isSingle); //OUTPUT: Boolean
119+
```
120+
121+
# Objects in JavaScript
122+
123+
**In JavaScript, EVERYTHING is an Object.**
124+
125+
Objects are the primitibe data structure in JavaScript. We define objects using curly brackets {} and keep key-value pairs seperated by comma.
126+
127+
```js
128+
const myself = {
129+
firstName: "Prabesh",
130+
lastName: "Thapa",
131+
email: "iamprabesh@proton.me",
132+
nickName: "entry",
133+
};
134+
```
135+
136+
## Create, Read, Update and Delete (CRUD) in Objects
137+
138+
### Create
139+
140+
```js
141+
// Creating an empty object
142+
let player = {};
143+
144+
player.name = "Cristiano Ronaldo";
145+
player["sports"] = "Football"; //Using [] is also valid
146+
player.nickname = "GOAT";
147+
player.country = "Portugal";
148+
149+
console.log(player);
150+
151+
// OUTPUT:
152+
// {
153+
// "name": "Cristiano Ronaldo",
154+
// "sports": "Football",
155+
// "nickname": "GOAT",
156+
// "country": "Portugal"
157+
// }
158+
```
159+
160+
Or, we can simply add all key-value pairs at oncce.
161+
162+
```js
163+
let player = {
164+
name: "Cristiano Ronaldo",
165+
sports: "Football",
166+
nickname: "GOAT",
167+
country: "Portugal",
168+
};
169+
```
170+
171+
### Read
172+
173+
We use the `.` operator to access the values of an object.
174+
175+
```js
176+
player.name; //OUTPUT: "Cristiano Ronaldo
177+
player.nickname; //OUTPUT: "GOAT"
178+
```
179+
180+
We can also use the square brackets `[]` to access these properties.
181+
182+
```js
183+
player["sports"]; //OUTPUT: "Football"
184+
player["country"]; //OUTPUT: "Portugal"
185+
```
186+
187+
### Update
188+
189+
We can update the value of a key by overriding the previous value.
190+
191+
```js
192+
player.name = "Cristiano Alveiro Dos Santos Ronaldo";
193+
```
194+
195+
### Delete
196+
197+
We can use the `delete` operator to remove a property from an object.
198+
199+
SYNTAX:
200+
201+
```
202+
delete object.property
203+
delete object[property]
204+
```
205+
206+
```js
207+
const user = {
208+
firstname: "Entry",
209+
lastname: "Eemroy",
210+
};
211+
212+
console.log(user.firstname);
213+
// OUTPUT: "Eemroy"
214+
215+
delete user.firstname;
216+
217+
console.log(user.firstname);
218+
// OUTPUT: undefined
219+
```
220+
221+
### Other Useful Methods to Know
222+
223+
**Print Keys from the object**
224+
225+
`Object.keys(player);`
226+
227+
```js
228+
["name", "sports", "nickname", "country"];
229+
```
230+
231+
**Print values from the object**
232+
233+
`Object.values(player);`
234+
235+
```js
236+
["Cristiano Ronaldo", "Football", "GOAT", "Portugal"];
237+
```
238+
239+
## Built-In JavaScript Objects
240+
241+
JavaScript offers four buil-in obejcts: `Array`, `Date`, `Math`, and `String`. All these objects have their own special purpose.
242+
243+
### Array Object
244+
245+
JavaScript Arrays are dynamically created special kind of object. Arrays store a numbers of variables including zero, which are known as empty array. These variables do not necesarily need to have same type. The variables stored in array have no names. These variables are accessed using non-negative integer index value.
246+
247+
If an array has `n` variables, we can say that the length of that array is `n`. These variables are referenced using integer index form `0` to `n-1`.
248+
249+
- Arrays are resizable and can contain mixture of different data types.
250+
- Arrays are zero-indexed i.e. the first element of array is at index `0`.
251+
- Copying arrays creat e a shallow copy, rather than deep copies.
252+
253+
**_A shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made. As a result, when you change either the source or the copy, you may also cause the other object to change too — and so, you may end up unintentionally causing changes to the source or copy that you don't expect `[mdn]`._**
254+
255+
**_A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made. As a result, when you change either the source or the copy, you can be assured you're not causing the other object to change too; that is, you won't unintentionally be causing changes to the source or copy that you don't expect`[mdn]`._**
256+
257+
#### Properties of an Array
258+
259+
Array object has one property, `length`. the `length` property indicates the number of component stores in that array.
260+
261+
Synatx:
262+
263+
```js
264+
arrayObject.length;
265+
```
266+
267+
Examples:
268+
269+
```js
270+
const arr1 = [1, 2, 3];
271+
const arr2 = new Array(9);
272+
const arr3 = new Array(-10); // Negative numbers are not allowed
273+
274+
console.log(arr1.length); // OUTPUT: 3
275+
276+
console.log(arr2.length); //OUTPUT: 9
277+
278+
console.log(arr3.length); //OUTPUT: RangeError: Invalid array length
279+
```
280+
281+
<!-- ?TODOS -->
282+
<!-- Prototypal Inheritance -->
283+
<!-- Object Protoype -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /