https://github.com/ufocoder/javascript.anomaly
Examples of not obvious behaviors for javascript beginner programmers
https://github.com/ufocoder/javascript.anomaly
anomaly closure javascript logic math
Last synced: 6 months ago
JSON representation
Examples of not obvious behaviors for javascript beginner programmers
- Host: GitHub
- URL: https://github.com/ufocoder/javascript.anomaly
- Owner: ufocoder
- License: mit
- Created: 2016-01-19T05:43:36.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-06-12T13:57:24.000Z (over 6 years ago)
- Last Synced: 2025-03-29T05:22:43.073Z (7 months ago)
- Topics: anomaly, closure, javascript, logic, math
- Homepage:
- Size: 10.7 KB
- Stars: 135
- Watchers: 8
- Forks: 18
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Javascript Anomaly Page
This page created for javascript beginner programmers to show not obvious behaviors, let's call all of them "anomalies"
* [Assignment Anomaly](#assignment-anomaly)
* [Reference Anomaly](#reference-anomaly)
* [New Anomaly](#new-anomaly)
* [Closure Anomaly](#closure-anomaly)
* [Context Anomay](#context-anomaly)
* [Delete Anomaly](#delete-anomaly)
* [Type Anomaly](#type-anomaly)
* [Compare Anomaly](#compare-anomaly)
* [Math Anomaly](#math-anomaly)
* [Logic Anomaly](#logic-anomaly)
* [Variable scope Anomaly](#variable-scope-anomaly)
* [Function Arguments Anomaly](#function-arguments-anomaly)
* [toString Anomaly](#tostring-anomaly)
* [New line Anomaly](#new-line-anomaly)
* [Variable hoisting Anomaly](#variable-hoisting-anomaly)### Assignment Anomaly
```
(function(){
var a = b = 3;
})();console.log(typeof a);
// output: undefinedconsole.log(typeof b);
// output: numbervar a={},
b={key:'b'},
c={key:'c'};a[b]=123;
a[c]=456;console.log(a[b]);
// output: 456
```### Reference Anomaly
```
var a = { value: 1 };
var b = a;
b.value = 2;console.log(a.value);
// output: 2
```### New Anomaly
```
var myClass = function() {
this.a = 1;
this.b = 2;
};var myClass2 = function() {
this.a = 1;
this.b = 2;return {
a: 2
};
};var myObject = new myClass();
var myObject2 = new myClass2();console.log(typeof myObject.b);
// output: number
console.log(typeof myObject2.b);
// output: undefined
```### Closure Anomaly
```
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 100);
}// output: 3
// output: 3
// output: 3
```### Context Anomaly
```
var message = {
content: 'Hello world!',
send: function () {
console.log(this.content)
}
};setTimeout(message.send);
// output: undefined
```### Delete Anomaly
```
var x = 1;
var output = (function(){
delete x;
return x;
})();console.log(output);
// output: 1
```### Type Anomaly
```
console.log(typeof null);
// output: objectconsole.log(null instanceof Object);
// output: falseconsole.log(typeof NaN);
// output: numberconsole.log(typeof function () {});
// output: function
// but there's no function type http://ecma-international.org/ecma-262/5.1/#sec-8
```### Compare Anomaly
```
console.log('' == '0');
// output: falseconsole.log(0 == '');
// output: trueconsole.log(0 == '0');
// output: trueconsole.log(false == 'false');
// output: falseconsole.log(false == '0');
// output: trueconsole.log(false == undefined);
// output: falseconsole.log(false == null);
// output: falseconsole.log(null == undefined);
// output: trueconsole.log(' \t\r\n ' == 0);
// output: trueconsole.log("abc" == new String("abc"));
// output: trueconsole.log("abc" === new String("abc"));
// output: falseconsole.log(0.1 + 0.2 == 0.3);
// output: false
// sum of float values is not equals obvious float valueconsole.log(NaN != NaN);
// output: trueconsole.log(NaN == NaN);
// output: falseconsole.log(NaN === NaN);
// output: falseconsole.log(!!undefined);
// output: falseconsole.log(!!NaN);
// output: falseconsole.log(!!null);
// output: falseconsole.log([1, 2, 3] == [1, 2, 3]);
// output: false
// How to detect array equality in JavaScript?console.log(new Array(3) == ",,");
// output: trueconsole.log(new Array(3) === ",,");
// output: falseconsole.log("a" > "b");
// output: falseconsole.log("abcd" < "abcd");
// output: falseconsole.log("abcd" < "abdc");
// output: trueconsole.log("123" > "13");
// output: false
```### Math Anomaly
```
console.log("2" * "3");
// output: 6console.log("2" * "3" + "4");
// output: "64"console.log("2" * "3" + "4" * "5")
// output: 26console.log("test " + 1);
// output: test 1console.log("test " + 1 + 1);
// output: test 11console.log("days" * 2);
// output: NaNconsole.log(null + null);
// output: 0console.log({} + {});
// output: [object Object][object Object]console.log({} + []);
// output: [object Object]console.log({} + 5);
// output: [object Object]5console.log([] + {});
// output: [object Object]console.log([] + []);
// output:
// will output empty string ''console.log([] + 5);
// output: 5console.log(++[[]][+[]]+[+[]]);
// output: "10"
```### Logic Anomaly
```
console.log(0 || 'a');
// output: aconsole.log(0 || undefined);
// output: undefinedconsole.log({} && 'a');
// output: aconsole.log(0 && 'a');
// output: 0
```### Variable scope Anomaly
```
function Foo(value) {
this.bar = value;
}
var test = new Foo('test');
console.log(test.bar);
// output: testFoo('test');
console.log(bar);
// output: test
```### Function Arguments Anomaly
```
(function (foo, bar) {
console.log(typeof arguments);
// output: objectarguments[0] = 999;
console.log(foo);
// output: 999
})(1, 2);
```### toString Anomaly
```
try {
eval("2.toString()")
} catch (err) {
console.log(err.message)
// output: Unexpected token ILLEGAL
}console.log(2..toString());
// output: 2console.log(2 .toString());
// output 2console.log((2).toString());
// output: 2console.log([1, 2, 3].toString())
// output: 1,2,3var a = {b: 2, c: 3};
console.log(a.toString())
// output: [object Object]
```### New line Anomaly
```
function foo() {
return "Yeah";
}
function bar() {
return
"Yeah";
}console.log(foo());
// output: Yeahconsole.log(bar());
// output:
```### Variable hoisting Anomaly
```
var a = 1;
function bar() {
if (!a) {
var a = 10;
}
console.log(a);
}
bar();
// output: 10
``````
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
console.log(a);
// output: 1
```Note that `anomaly` word has only literary turnover, it's not a technical term.