Hey Coders!!๐ค
In the first part we have learned about :Printing Statements, Variables in JS and Arithmetic Operators!!
So, This is the Part-2 of Javascript for Beginners!! And if you have not seen my Part-1 go watch now because in that one we have talked about the basic things in JS!๐
Topic:๐
Incrementing Numbers๐
Hopefully that went well!
- Now can we make this more easy??
We can use the
+=
it's the short and easy way to add something!! LIKE THIS๐
var a = 10
var b = 20
var c = "I am a"
console.log(a += 10)
console.log(b += 10)
console.log(c += " string")
Output:
- 20
- 30
- I am a string
Decrementing Numbers๐งฉ
To do decrement numbers you can use (-=
) LIKE THIS๐
var a = 10
var b = 20
var c = 30
console.log(a -= 10)
console.log(b -= 10)
console.log(c -= 10)
Output:
- 0
- 10
- 20
Shortcuts :(*=, /=
)๐ก
var a = 10
var b = 20
var c = 30
console.log(a *= 10)
console.log(b *= 10)
console.log(c *= 10)
Output:
100
200
300
NOW TRY TO DO /
ON YOUR OWN
Add 1 to number๐
Now what if you wanna add 1 to the number? OfCourse you guys will do: LIKE THIS ๐
var coding= 100
coding= coding+ 1
console.log(coding)
Output:
- 101
This one is not wrong!
BUT, BUT, BUT we have a more easy way. LIKE THIS๐
var coding = 100
coding++
console.log(coding)
Output:
- 101
Here, we have done it by ++
because in JS we'll find such interesting and easy ways!
Similarly you can do this method with (--)
also!! (Try on your own!)
FUNFACT: After doing Javascript we think that we are actually doing coding!!๐
Escape sequences in strings๐
Now I'll give some examples related to these CODE OUTPUTS
๐ :
- So, If you want an output with the
single quote
then you can use\'
. Means if you want an output like this :-cookie'
then you must use\'
!!
- And if you want to have a output with the
Double quotes
then you can use\"
. if you want an output like this :-cookie"
then you must use:\"
As shown in figure!!
- Also if you an output with a
backslash
you can use\\
. if you want an output like this :-cookie\
then you can use\\
.
- Similarly, if you want an output with a
new line
you must use\n
. EXAMPLE:var cookie = "cookies\nare the best!!" console.log(cookie)
OUTPUT :
- Now, we'll talk about
\r
in JS it's thecarriage return
. A carriage return is a type of escape character that will reset the position of the cursor to the beginning of a line of text.
EXAMPLE:
console.log("Hello coders!")
console.log("Hello \rcoders!")
OUTPUT:
- Now, if you want an output with the
tab
so you can do\t
.
EXAMPLE:
var cookie = "cookies \t are the best!!"
console.log(cookie)
OUTPUT:
- Now, we'll talk about the
backspace
in JS.
EXAMPLE:
var cookie = "cookies\b\b\b are the best!!"
console.log(cookie)
OUTPUT:
Hopefully that went well!!๐
- Now you'll be having a homework of
form feed
(ON YOUR OWN) All the best!!
Motivation for today: Any fool can write code that a computer can understand...๐
If you found this article helpful comment a ๐๐. So, this was my Javascript for beginners Part-2. Now, I will see you all again tomorrow in my Part-3 till then byee!!๐