DEV Community

imsabir
imsabir

Posted on • Updated on

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

Top comments (8)

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
 
_bkeren profile image
''
  1. Nothing
  2. 0 2 2
Collapse
 
msabir profile image
imsabir

Updated the answers folks

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
 
gikdev profile image
Mohammad Mahdi Bahrami

You need to learn grammar...

Collapse
 
irondev25 profile image
Rahul Bhaskar

Nothing
1 1 2

Collapse
 
drmikecrowe profile image
drmikecrowe

Nothing
0 3 3

Collapse
 
angelinajolly profile image
Info Comment hidden by post author - thread only accessible via permalink
AngelinaJolly

how to get the proper ascending order ? Kerala vasiyam specialist in Chennai

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