Day 9: JavaScript Challenge
It’s a demo-less post. Nothing to show but a lot to explain. We are going to play with the console.
Before this challenge, I only made use of console.log
for debugging.
To me, that was enough to make sure the outputs of certain program, but there
are other built-in methods from native console
object available to help us
eradicate bugs and errors. We are going to make the most of it.
To increase my own understanding, I’ll take all examples here personal. I hope you don’t mind.
Regular Logging
This is regular console.log
we often encounter in our spaghetti code.
With Interpolation
Sometime we need to insert value between text consumed by console.log
.
Well, what for? To make it easier to change later on!
Styling The Text
If we would love to emphasize text with color and style, don’t hesitate. Nike says, “Just do it!”
You will see this in console
.
メリー メディオーアティ is the best ❤
Pay attention. %c
before text indicates the text are going to be styled with
inline CSS we pass to second parameter.
Warning!
Instead of simply “say what you say” via console.log
, what if we warn our
users about the one we care the most?
Information
We’ve warned users before, don’t you think we need to kindly inform users about something too?
Asserting
We also can check the truth via console
. It’s quite useful.
console.assert
only display text whenever the condition false
like that
expression above. Of course I like ちえさ せれな, I mean it!
Displaying Error
Enough warning, enough conveying, just tell the users that we made a terrible mistake even if it was a beautiful mistake! Error! Error! Error!
Clearing
Back then, I liked ちえさ せれな, really liked her. Now, I want to forget that feeling… It’s going to be cleared away… Hopefully.
Timing
How long does it take to fetch my profile from github?
Grouping
The last one, I want to group my crushes with pride!
Here’s my girls!
Here’s how it should be done!
Copy-paste all the snippets above and run in console. CTRL
+ SHIFT
+ i
to
open the console
.
Okay, that’s it for today. Until tomorrow.