DEV Community

imsabir
imsabir

Posted on β€’ Edited on

10 3

Weird JavaScript - Part 1 πŸ§‘β€πŸ’»

Hey I am Javascript!!
Instead Weird Javascript.

Its look simple but its not !!

As being said, lets move straight to see some Weird parts:

Note: You can comment your answers

WJS - 1

function bark() {
  console.log('πŸ• barkk barkk!πŸ• ');
}

bark.animal = 'dog';
Enter fullscreen mode Exit fullscreen mode

πŸ”₯πŸ”₯ What you think it will output πŸ‘¨β€πŸ’» ?
Answer:
with bark.animal=dog, one more element is get added to function bark(), don't be surprise, Everthing is Object in JavaScript and thats why its wired

One more
WJS - 2

let number = 0;
console.log(number++);
console.log(++number);
console.log(number);
Enter fullscreen mode Exit fullscreen mode

πŸ”₯πŸ”₯ What you think it will output πŸ‘¨β€πŸ’» ?
Answer:

let number = 0;
console.log(number++); // Output : 0 and then increment it by 1 its known as return then increment
console.log(++number); // Output: 2 AND its increment then return
console.log(number); // it will log latest value of number i.e. 2
0 2 2
Enter fullscreen mode Exit fullscreen mode

Follow @msabir for more

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (10)

Collapse
 
taiko profile image
Taiko β€’

i don't think there is anything weird with WJS-2 example. These operators are supposed to work like this in most (if not all) programming language.
num++ -> return then increment
++num -> increment then return

Collapse
 
temiloluwa_banks_4fb5fa6f profile image
Temiloluwa Banks β€’

Very amateur like

Collapse
 
msabir profile image
imsabir β€’

Updated the answers folks

Collapse
 
_bkeren profile image
'' β€’
  1. Nothing
  2. 0 2 2
Collapse
 
msabir profile image
imsabir β€’

Follow @msabir for more such weird parts!! Also, You will receive daily update of why JS works like this,** JS under the hood ** article which I will be publishing in some time. Cheers!!

Collapse
 
irondev25 profile image
Rahul Bhaskar β€’

Nothing
1 1 2

Collapse
 
drmikecrowe profile image
drmikecrowe β€’

Nothing
0 3 3

Collapse
 
gikdev profile image
Mohammad Mahdi Bahrami β€’

You need to learn grammar...

Collapse
 
temiloluwa_banks_4fb5fa6f profile image
Temiloluwa Banks β€’

Exactly....very poor grammar I must say

Collapse
 
angelinajolly profile image
AngelinaJolly β€’
Comment hidden by post author

Some comments have been hidden by the post's author - find out more

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay