Monthly Archives: March 2016

ES6 Variable Swap Explained

The following ES6 code swaps the values of two variables:

var x = 1;
var y = 2;
[x, y] = [y, x];

It works by using a Destructuring assignment on the array [y, x]. Destructuring is used to extract values from an array or an object. You are able to do this for example:

var [month, year] = [5, 1990]

The example above creates two local variables called “month” and “year” with the respective values 5 and 1990. In the swap example we are omitting the “var” keyword and end up with two simple assignment calls (most ES6 transpilers end up creating the following code):

var x = 1;
var y = 2;
var temp = [x, y];
x = temp[1];
y = temp[0];

How to Get the MAC Address From a Local IP Address

Command

ping -c 1 192.168.1.1 > /dev/null && arp -a | grep 192.168.1.1

This returns something like this:

? (192.168.1.1) at 61:02:6a:40:34:A3 on en0 ifscope [ethernet]

How does this work?

The command pings the IP address once to make sure that your machine and the target machine communicated at least once. Because we don’t need the output of the ping command, we can simply pipe it into /dev/null. It then continues and loads all entries from ARP (Address Resolution Protocol) and “greps” the line which matches with the provided IP address.

Careful with IPv6!

ARP is IPv4 only. For IPv6 you need to use NDP (Neighbor Discovery Protocol). Simply replace “arp -a” with “ndp -a” above.