JavaScript for Beginners!   Part-2๐Ÿ‘‘

JavaScript for Beginners! Part-2๐Ÿ‘‘

ยท

3 min read

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๐ŸŽ€

image.png

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๐ŸŽƒ

image.png

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 :

image.png

  • Now, we'll talk about \r in JS it's the carriage 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:

image.png

  • 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:

image.png

  • Now, we'll talk about the backspace in JS.

EXAMPLE:

var cookie = "cookies\b\b\b are the best!!"
console.log(cookie)

OUTPUT:

image.png

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!!๐Ÿ’™

ย