You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/technologies/javascript/working-with-classes-and-objects.mdx
+76-16Lines changed: 76 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,9 +9,10 @@ import SubHeading from "@site/src/components/SubHeading";
9
9
10
10
As you begin your journey into the world of JavaScript and programming, you'll realize that variables, objects, and classes are at the heart of the language. They are the building blocks of any good JavaScript program, and mastering them will set you on the path to becoming a true JavaScript ninja!
A variable is simply an identifier or container that holds a value. It can contain data of different types, such as boolean, string, number, object, and array.
16
17
17
18
> 💡 A boolean variable holds either True or False, a string variable contains a sequence of characters enclosed in quotation marks, and an array is a collection of values of any data type.
@@ -20,10 +21,16 @@ A variable is simply an identifier or container that holds a value. It can conta
20
21
21
22
From the image above, the cup represents an empty variable; the milk and coffee are data of different data types. When you put the data into the cup, the cup becomes a variable of data type, coffee or milk.
22
23
23
-
## How to create variables in JavaScript
24
+
<br />
25
+
26
+
## `Create variables` in JavaScript
27
+
24
28
To declare (create) variables in JavaScript, you need to use some built-in keywords such as var, let, and const. Let's talk briefly about them.
25
29
26
-
### The var keyword
30
+
<br />
31
+
32
+
### 👉 The `var` keyword
33
+
27
34
The var keyword is the oldest way of declaring variables in JavaScript, and it's not advisable to use it in your JavaScript programs because it is not block scope.
28
35
29
36
> A variable is block-scoped when it only exists within the curly braces where it was declared and cannot be accessed outside the curly braces.
@@ -41,7 +48,11 @@ if(true) {
41
48
console.log(myAge) //prints 20
42
49
console.log(yourAge) //ReferenceError: yourAge is not defined (Block-scoped)
43
50
```
44
-
### The let keyword
51
+
52
+
<br />
53
+
54
+
### 👉 The `let` keyword
55
+
45
56
The let keyword is a suitable alternative to var. It is block-scoped and used for declaring variables whose values may change at some point during execution.
46
57
```js
47
58
let age =20; //sets a variable to 20
@@ -50,7 +61,10 @@ console.log(age); // prints 5 to the console
50
61
```
51
62
The code snippet above shows a variable created using the `let` keyword with an initial value of 20, then changed to 5.
52
63
53
-
### The const keyword
64
+
<br />
65
+
66
+
### 👉 The `const` keyword
67
+
54
68
The const keyword is used to declare variables that cannot be re-assigned to another value. It holds constant values.
55
69
```js
56
70
constage=20;
@@ -59,7 +73,10 @@ console.log(age) //returns a TypeError: Assignment to constant variable. (Inval
59
73
```
60
74
> 💡 Don’t use the `var` keyword. Use `const` when you need to declare a constant and `let` when the variable can be re-assigned to another value.
61
75
62
-
### Naming variables in JavaScript
76
+
<br />
77
+
78
+
### 👉 `Naming variables` in JavaScript
79
+
63
80
When naming variables in JavaScript, there are a few things you need to consider.
64
81
- Variables can only start with a letter, underscore, or dollar sign. Numbers and other symbols can come after the first position.
65
82
```js
@@ -76,7 +93,11 @@ const const = 24;
76
93
/*-- They are all invalid naming conventions --*/
77
94
```
78
95
- Use variable names that describe the value they hold or contain.
79
-
## What are objects in JavaScript?
96
+
97
+
<br />
98
+
99
+
## `What are objects` in JavaScript?
100
+
80
101
An object is an entity that has a collection of properties arranged in key-value pairs. The key is a property name, and every property has a value assigned to it within the object.
The code snippet above creates a JavaScript object for the cup. The name of the JavaScript object is `cup`, and its properties are the key-value pairs.
93
114
> 💡 An object can have properties of different data types. The cup object can also have a property of `isBlack: false`.
94
115
95
-
### Accessing the object properties in JavaScript
116
+
<br />
117
+
118
+
### 👉 `Accessing object properties` in JavaScript
119
+
96
120
You can access the property of an object using the dot notation or bracket notation.
121
+
97
122
```js
98
123
//👇🏻 Dot notation
99
124
console.log(cup.colour) //Output: white
@@ -105,9 +130,11 @@ console.log(cup["colour"]) //Output: white
105
130
console.log(cup["material"]) //Output: porcelain
106
131
console.log(cup["size"]) ////Output: medium
107
132
```
133
+
108
134
> 💡 In dot notation, you can access each property by using the syntax `object.propertyname`, and in bracket notation, use `object["propertyname"]`.
109
135
110
136
Use camel case to join property names that are more than one word.
137
+
111
138
```js
112
139
constcup= {
113
140
colour:"white",
@@ -119,7 +146,9 @@ const cup = {
119
146
console.log(cup.dateCreated) //Output is 02-05-2023
120
147
console.log(cup["dateCreated"]) //Output is 02-05-2023
121
148
```
149
+
122
150
In JavaScript objects, if a property name is spaced, you need to put it in quotes, and you can only access its value using the bracket notation.
151
+
123
152
```js
124
153
constcup= {
125
154
colour:"white",
@@ -130,8 +159,13 @@ const cup = {
130
159
131
160
console.log(cup["date created"]) //Output is 02-05-2023
132
161
```
133
-
### Modifying JavaScript objects
162
+
163
+
<br />
164
+
165
+
### 👉 `Modifying` JavaScript objects
166
+
134
167
A JavaScript object property can be modified by referencing and updating its value. For example, let’s change the material and colour of the cup.
168
+
135
169
```js
136
170
constcup= {
137
171
colour:"white",
@@ -154,6 +188,7 @@ console.log(cup)
154
188
The code snippet above changes the values of the object properties - colour and material.
155
189
156
190
Recall that the `const` keyword creates constant variables. Therefore, you can not re-assign an object created with the `const` keyword to another value, but you can change its properties.
191
+
157
192
```js
158
193
constcup= {
159
194
colour:"white",
@@ -171,15 +206,20 @@ cup = {
171
206
172
207
console.log(cup) //returns a TypeError: Assignment to constant variable
173
208
```
209
+
174
210
> 💡 You can only re-assign an object to another value when it is declared using the `let` keyword.
175
211
176
-
## What are classes in JavaScript?
212
+
<br />
213
+
214
+
## `What are classes` in JavaScript?
215
+
177
216
A class is a blueprint for creating objects that have the same properties and functions. It provides a template for creating multiple objects with similar features but different values.
178
217
179
218

180
219
181
220
The image above shows a class of domestic animals containing a dog and a cat. The class provides the blueprint that determines the kind of animals present.
182
221
Here is an example of a JavaScript class describing the image above.
To create a class in JavaScript, you need to add the `class` keyword followed by the name of the class name in sentence case.
243
+
202
244
```js
203
245
classClassName {}
204
246
```
247
+
205
248
Next, add a `constructor` method containing the class attributes.
249
+
206
250
```js
207
251
classDomestic {
208
252
constructor(name, colour, owner) {
@@ -212,16 +256,23 @@ class Domestic {
212
256
}
213
257
}
214
258
```
259
+
215
260
From the code snippet above, the class - `Domestic` has name, colour, and owner attributes. The `this` keyword reference any current object initiated from the class and sets its value to the object's parameter.
216
261
217
262
Finally, you can create new object instances of the class by using the `new` keyword and passing the constructor parameters into the class.
The code snippet above changes the name of the cat and dog object after initiation.
240
291
292
+
<br />
293
+
241
294
## Key Takeaways
242
-
- Variables are like containers that hold data. They allow you to store and manipulate data of different types, such as numbers, strings, or boolean values.
243
-
- Objects, on the other hand, are like containers that hold multiple pieces of information - properties. These properties can be anything from strings and numbers to other objects and arrays.
244
-
- Classes are templates for objects. They provide a blueprint that you can use to create multiple objects with the same properties and behaviours.
295
+
296
+
-`Variables` are like containers that hold data. They allow you to store and manipulate data of different types, such as numbers, strings, or boolean values.
297
+
-`Objects`, on the other hand, are like containers that hold multiple pieces of information - properties. These properties can be anything from strings and numbers to other objects and arrays.
298
+
-`Classes` are templates for objects. They provide a blueprint that you can use to create multiple objects with the same properties and behaviours.
245
299
246
300
Understanding what variables, objects, and classes are, is essential for a beginner in JavaScript. By mastering these basic concepts, you're well on your way to building more complex programs in JavaScript.
247
301
248
-
Thank you for reading! 🎉
302
+
<br />
303
+
304
+
## Resources
305
+
306
+
- 👉 [JavaScript Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript) by Mozilla Foundation
307
+
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
308
+
- 👉 [Custom Development Services](https://appseed.us/custom-development/) provided by experts
0 commit comments