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';
π₯π₯ 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);
π₯π₯ 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
Follow @msabir for more
Top comments (10)
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
Very amateur like
Updated the answers folks
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!!
You need to learn grammar...
Exactly....very poor grammar I must say
Nothing
0 3 3
Nothing
1 1 2
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