Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
Hacking away...

Raju Mandapati therajumandapati

👨‍💻
Hacking away...
View GitHub Profile
@therajumandapati
therajumandapati / ga-redirect-tracking.html
Last active May 2, 2018
GA Tracking before redirection
View ga-redirect-tracking.html
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX');
</script>
<script>
function redirect(url) {
@therajumandapati
therajumandapati / coercion.js
Created Jun 10, 2015
JavaScript for Amateurs
View coercion.js
+[] evaluates to the number zero, because unary-plus tries to convert any value to a number, so that leaves
++[[]][0]+[0]
[[]][0] is an expression accessing the first element in the array, which is a reference to the inner array. This is shorthand for x = [[]]; x[0];, if it helps you see it better.
++[[]][0] then evaluates to a pre-increment of the first element in the array, which returns the value after the element is incremented. Because an array is not numeric, it first needs to coerce it to a number. We saw before that an empty array coerces to 0, so incrementing 0 is 1. If we had a reference to [[]], we’d see that it becomes [1] after the increment operation. (i.e. x = [[]]; ++x[0] // returns 1, and x is now [1])
So, this whole expression boils down to 1 + [0]. Since a number and an array can’t normally be added together, javascript converts both values to strings and concatenates them.