You are starting coding JavaScript.. How do you know if you are writing good or bad JS?
This is a mini-guide to get started with JavaScript.
Ok, let's begin with this cool guide full of tips & tricks. Grab some
Table Of Contents ๐ฆ
- Variables
- Spot the mystical NaN
- How to type-check objects
- Function default parameters
- Rest parameters
- Callback functions
- The "this" keyword
- Shorthand properties
- Destructuring object properties
- Value existence check
- Property existence check
- Shallow copy objects
- Deep copy objects
- Destructuring arrays
Variables
Use let
to define variables that can potentially change, use const
for variables that should not change. Nowadays you should never use var
.
Understand let & const
Use let
for variables that are supposed to change at some point in your app.
Use const
for variables which should never change.
Javascript variables โน๏ธ
Understand the old var
You should no longer use var
. ๐
โโ๏ธ
The old "var" โน๏ธ
Spot the mystical NaN
NaN means Not-A-Number. ๐คฏ
This the best way to check if a number is NaN or not.
- Line (1): Tries to parse string
55px
as Number, but because it can't be parsed it returnsNaN
, soresult
isNaN
. - Line (3): Checks if result is
NaN
, which is true. - Line (4): Checks if
'this is false baby!'
isNaN
, which is false because it's aString
not aNaN
. - Line (5): Checks if
NaN
is aNaN
, which is true because it's aNaN
.
How to type-check objects
This is the best way to check your object types.
Function default parameters
This is the best way to define default parameters for your functions.
- Line (1): By using
name =
it allows you to define a default parameter for your function arguments. - Line (5): Overrides the default parameter.
- Line (6): Use the default parameter.
Rest parameters
A function can be called with any number of arguments, no matter how it is defined.
Rest parameters and spread operator โน๏ธ
Still not understanding? Yeah, I was there too. ๐คท
Basically this allows you to send as many arguments you want, they will end up converted into an array. Let's see this in action โก
- Line (1): By using
...prices
it allows you pass an arbitrary amount of attributes - Line (7): Call the function with
10, 50, 35, 42, 28
as argument.
But say you want extra parameters...
- Line (1): By using
...prices
it allow pass an arbitrary amount of attributes - Line (6): Call the function with
'Jhon', 'jhon@example.com', 10, 50, 35, 42, 28
as argument.
NOTE: Extra parameters go first, the others go after (those are called "the rest" ๐).
Callback functions
Ok kids, this is important. So, you better pay attention ๐จโ๐ซ
A function passed as an argument to another function, usually to be called as a result of an event occurring.
Introduction: callbacks โน๏ธ
As you can see, we have defined a function that takes a name and a function as parameters.
sayHello
logs "Hello, Jhon" AND then calls the function sayGoodbye
which logs "Goodbye, Jhon". Notice that sayGoodbye expects the name argument.
The "this" keyword
Alright, this one is tricky. It's quite a big topic, so... I'm redirecting you to one of my posts: The biggest JavaScript OOP gotcha ๐
Object methods, "this" โน๏ธ
Shorthand properties
- Line (7): Use the variable
name
to assign aproperty
"name" with the value of thename
variable. - Line (8): Use the variable
beer
to assign akey
called "gas" with the value of thegas
variable.
If you don't understand, have some beers and come back later ๐ป
Destructuring object properties
Hopefully you are still sober ๐ฅด
Destructuring assignment is a special syntax that allows us to โunpackโ arrays or objects into a bunch of variables, as sometimes thatโs more convenient. Destructuring also works great with complex functions that have a lot of parameters, default values, and so on.
Destructuring assignment โน๏ธ
- Line (10): Define an object, don't worry it will be used as a way to define our new variables which means this object is not assigned anywhere.
- Line (11): Take the property
name
fromrobot
and define a variablename
. - Line (12): Take the property
stealing
fromrobot
.skills
and define a variablestealing
. - Line (13): Take the rest of the
robot
properties and assign them to a variablerest
. - Line (14): Use
robot
to define the variables.
Value existence check
This is the best way to check if a value exists on a given object.
- Line (6): Checks if there is a property on the computer object which value is equal to
127.0.0.1
Property existence check
There are couple ways to check the presence of a property on an object.
- Line (5): Performs poorly when the object has too many keys.
- Line (6): Performs well even if the object has too many keys.
Shallow copy objects
A shallow copy will duplicate the top-level properties, but the nested object is shared between the original(source) and the copy(target).
Let's experiment:
- Line (9): Make a shallow copy of the
robot
object. - Line (19): Update the
robotClone
speed to800
.
๐ WOOT ๐ we have altered both robots speed
, both robotClone.skills
and robot.skills
share the same reference to the object because of individual copies were not made, instead a reference to the object was copied. Any change made to any of the object's property applies to all references using the object.
NOTE: Don't shallow copy when your object has nested properties (Unless you really want to have this behaviour), instead use Deep copy.
Deep copy objects
This is the best way to do deep copy.
By using Deep copy VS Shallow copy you can notice there are no side effects on the original object ๐
Destructuring arrays
This is very similar to Destructuring objects but because arrays do not have keys but instead positions then we need to access the positions we want to get back.
- Line (1): Defines a multi-dimentional array (You can also use a regular array as well, but I want to show you the power of this thing ๐ช).
- Line (3): We define a variable
firstRobotName
which value will berobots[0]
, that is['Bender', 'Steal']
. Then define a variablesecondRobotState
(notice we skip the robot name but take its state'Cool'
). Finally define a variablerest
to take the rest of the array.
Keep learning ๐ค
Final thoughts and next steps ๐
We had experimented with modern concepts that you should understand as a beginner, some of these tips & tricks are today "best way" to accomplish what we traditionally did with JavaScript in the past.
I had a good time writing this post, and I wish you learnt a lot from it. For now, save this in your bookmarks so you have a quick reference to daily-basis JS tips.
I will love to hear about what other things you want to learn so I can include them in Part 2 of Modern JavaScript beginners guide which is going to be full of really cool stuff. ๐
Top comments (2)
NaN is Not A Number but NaN have type number ๐๐๐
Great ๐