<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tania Rascia</title>
    <description>The latest articles on DEV Community by Tania Rascia (@taniarascia).</description>
    <link>https://dev.to/taniarascia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F102715%2F04ebb11d-787e-4c32-a7a1-2056b7ea1347.jpg</url>
      <title>DEV Community: Tania Rascia</title>
      <link>https://dev.to/taniarascia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taniarascia"/>
    <language>en</language>
    <item>
      <title>Understanding This, Bind, Call, and Apply in JavaScript</title>
      <dc:creator>Tania Rascia</dc:creator>
      <pubDate>Mon, 28 Oct 2019 19:14:13 +0000</pubDate>
      <link>https://dev.to/digitalocean/understanding-this-bind-call-and-apply-in-javascript-dla</link>
      <guid>https://dev.to/digitalocean/understanding-this-bind-call-and-apply-in-javascript-dla</guid>
      <description>&lt;p&gt;&lt;em&gt;The author selected the &lt;a href="https://www.brightfunds.org/open-internet-free-speech" rel="noopener noreferrer"&gt;Open Internet/Free Speech Fund&lt;/a&gt; to receive a donation as part of the &lt;a href="https://do.co/w4do-cta" rel="noopener noreferrer"&gt;Write for DOnations&lt;/a&gt; program.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;&lt;code&gt;this&lt;/code&gt;&lt;/a&gt; keyword is a very important concept in JavaScript, and also a particularly confusing one to both new developers and those who have experience in other programming languages. In JavaScript, &lt;code&gt;this&lt;/code&gt; is a reference to an object. The object that &lt;code&gt;this&lt;/code&gt; refers to can vary, implicitly based on whether it is global, on an object, or in a constructor, and can also vary explicitly based on usage of the &lt;code&gt;Function&lt;/code&gt; prototype methods &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt;, and &lt;code&gt;apply&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although &lt;code&gt;this&lt;/code&gt; is a bit of a complex topic, it is also one that appears as soon as you begin writing your first JavaScript programs. Whether you’re trying to access an element or event in &lt;a href="https://www.digitalocean.com/community/tutorial_series/understanding-the-dom-document-object-model" rel="noopener noreferrer"&gt;the Document Object Model (DOM)&lt;/a&gt;, building classes for writing in the object-oriented programming style, or using the properties and methods of regular objects, you will encounter &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this article, you’ll learn what &lt;code&gt;this&lt;/code&gt; refers to implicitly based on context, and you’ll learn how to use the &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt;, and &lt;code&gt;apply&lt;/code&gt; methods to explicitly determine the value of &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implicit Context
&lt;/h2&gt;

&lt;p&gt;There are four main contexts in which the value of &lt;code&gt;this&lt;/code&gt; can be implicitly inferred:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  the global context&lt;/li&gt;
&lt;li&gt;  as a method within an object&lt;/li&gt;
&lt;li&gt;  as a constructor on a function or class&lt;/li&gt;
&lt;li&gt;  as a DOM event handler&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Global
&lt;/h3&gt;

&lt;p&gt;In the global context, &lt;code&gt;this&lt;/code&gt; refers to the &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Global_object" rel="noopener noreferrer"&gt;global object&lt;/a&gt;. When you’re working in a browser, the global context is would be &lt;code&gt;window&lt;/code&gt;. When you’re working in Node.js, the global context is &lt;code&gt;global&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you are not yet familiar with the concept of scope in JavaScript, please review &lt;a href="https://www.digitalocean.com/community/tutorials/understanding-variables-scope-hoisting-in-javascript" rel="noopener noreferrer"&gt;Understanding Variables, Scope, and Hoisting in JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the examples, you will practice the code in the browser’s Developer Tools console. Read &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-use-the-javascript-developer-console" rel="noopener noreferrer"&gt;How to Use the JavaScript Developer Console&lt;/a&gt; if you are not familiar with running JavaScript code in the browser.&lt;/p&gt;

&lt;p&gt;If you log the value of &lt;code&gt;this&lt;/code&gt; without any other code, you will see what object &lt;code&gt;this&lt;/code&gt; refers to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(this)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that &lt;code&gt;this&lt;/code&gt; is &lt;code&gt;window&lt;/code&gt;, which is the global object of a browser.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://www.digitalocean.com/community/tutorials/understanding-variables-scope-hoisting-in-javascript" rel="noopener noreferrer"&gt;Understanding Variables, Scope, and Hoisting in JavaScript&lt;/a&gt;, you learned that functions have their own context for variables. You might be tempted to think that &lt;code&gt;this&lt;/code&gt; would follow the same rules inside a function, but it does not. A top-level function will still retain the &lt;code&gt;this&lt;/code&gt; reference of the global object.&lt;/p&gt;

&lt;p&gt;You write a top-level function, or a function that is not associated with any object, like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printThis() {
  console.log(this)
}

printThis()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even within a function, &lt;code&gt;this&lt;/code&gt; still refers to the &lt;code&gt;window&lt;/code&gt;, or global object.&lt;/p&gt;

&lt;p&gt;However, when using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode" rel="noopener noreferrer"&gt;strict mode&lt;/a&gt;, the context of &lt;code&gt;this&lt;/code&gt; within a function on the global context will be &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'use strict'

function printThis() {
  console.log(this)
}

printThis()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generally, it is safer to use strict mode to reduce the probability of &lt;code&gt;this&lt;/code&gt; having an unexpected scope. Rarely will someone want to refer to the &lt;code&gt;window&lt;/code&gt; object using &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For more information about strict mode and what changes it makes regarding mistakes and security, read the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode" rel="noopener noreferrer"&gt;Strict mode&lt;/a&gt; documentation on MDN.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  An Object Method
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://www.digitalocean.com/community/tutorials/understanding-objects-in-javascript#properties-and-methods" rel="noopener noreferrer"&gt;method&lt;/a&gt; is a function on an object, or a task that an object can perform. A method uses &lt;code&gt;this&lt;/code&gt; to refer to the properties of the object.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const america = {
  name: 'The United States of America',
  yearFounded: 1776,

  describe() {
    console.log(`${this.name} was founded in ${this.yearFounded}.`)
  },
}

america.describe()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"The United States of America was founded in 1776."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;this&lt;/code&gt; is the same as &lt;code&gt;america&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In a nested object, &lt;code&gt;this&lt;/code&gt; refers to the current object scope of the method. In the following example, &lt;code&gt;this.symbol&lt;/code&gt; within the &lt;code&gt;details&lt;/code&gt; object refers to &lt;code&gt;details.symbol&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const america = {
  name: 'The United States of America',
  yearFounded: 1776,
  details: {
    symbol: 'eagle',
    currency: 'USD',
    printDetails() {
      console.log(`The symbol is the ${this.symbol} and the currency is ${this.currency}.`)
    },
  },
}

america.details.printDetails()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"The symbol is the eagle and the currency is USD."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way of thinking about it is that &lt;code&gt;this&lt;/code&gt; refers to the object on the left side of the dot when calling a method.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Function Constructor
&lt;/h3&gt;

&lt;p&gt;When you use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new" rel="noopener noreferrer"&gt;&lt;code&gt;new&lt;/code&gt;&lt;/a&gt; keyword, it creates an instance of a constructor function or class. Function constructors were the standard way to initialize a user-defined object before the &lt;code&gt;class&lt;/code&gt; syntax was introduced in the ECMAScript 2015 update to JavaScript. In &lt;a href="https://www.digitalocean.com/community/tutorials/understanding-classes-in-javascript" rel="noopener noreferrer"&gt;Understanding Classes in JavaScript&lt;/a&gt;, you will learn how to create a function constructor and an equivalent class constructor.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Country(name, yearFounded) {
  this.name = name
  this.yearFounded = yearFounded

  this.describe = function() {
    console.log(`${this.name} was founded in ${this.yearFounded}.`)
  }
}

const america = new Country('The United States of America', 1776)

america.describe()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"The United States of America was founded in 1776."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this context, &lt;code&gt;this&lt;/code&gt; is now bound to the instance of &lt;code&gt;Country&lt;/code&gt;, which is contained in the &lt;code&gt;america&lt;/code&gt; constant.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Class Constructor
&lt;/h3&gt;

&lt;p&gt;A constructor on a class acts the same as a constructor on a function. Read more about the similarities and differences between function constructors and ES6 classes in &lt;a href="https://www.digitalocean.com/community/tutorials/understanding-classes-in-javascript" rel="noopener noreferrer"&gt;Understanding Classes in JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Country {
  constructor(name, yearFounded) {
    this.name = name
    this.yearFounded = yearFounded
  }

  describe() {
    console.log(`${this.name} was founded in ${this.yearFounded}.`)
  }
}

const america = new Country('The United States of America', 1776)

america.describe()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in the &lt;code&gt;describe&lt;/code&gt; method refers to the instance of &lt;code&gt;Country&lt;/code&gt;, which is &lt;code&gt;america&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"The United States of America was founded in 1776."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A DOM Event Handler
&lt;/h3&gt;

&lt;p&gt;In the browser, there is a special &lt;code&gt;this&lt;/code&gt; context for event handlers. In an event handler called by &lt;code&gt;addEventListener&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; will refer to &lt;code&gt;event.currentTarget&lt;/code&gt;. More often than not, developers will simply use &lt;code&gt;event.target&lt;/code&gt; or &lt;code&gt;event.currentTarget&lt;/code&gt; as needed to access elements in the DOM, but since the &lt;code&gt;this&lt;/code&gt; reference changes in this context, it is important to know.&lt;/p&gt;

&lt;p&gt;In the following example, we’ll create a button, add text to it, and append it to the &lt;a href="https://www.digitalocean.com/community/tutorial_series/understanding-the-dom-document-object-model" rel="noopener noreferrer"&gt;DOM&lt;/a&gt;. When we log the value of &lt;code&gt;this&lt;/code&gt; within the event handler, it will print the target.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const button = document.createElement('button')
button.textContent = 'Click me'
document.body.append(button)

button.addEventListener('click', function(event) {
  console.log(this)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
&amp;lt;button&amp;gt;Click me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you paste this into your browser, you will see a button appended to the page that says “Click me”. If you click the button, you will see &lt;code&gt;&amp;lt;button&amp;gt;Click me&amp;lt;/button&amp;gt;&lt;/code&gt; appear in your console, as clicking the button logs the element, which is the button itself. Therefore, as you can see, &lt;code&gt;this&lt;/code&gt; refers to the targeted element, which is the element we added an event listener to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explicit Context
&lt;/h2&gt;

&lt;p&gt;In all of the previous examples, the value of &lt;code&gt;this&lt;/code&gt; was determined by its context—whether it is global, in an object, in a constructed function or class, or on a DOM event handler. However, using &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, or &lt;code&gt;bind&lt;/code&gt;, you can explicitly determine what &lt;code&gt;this&lt;/code&gt; should refer to.&lt;/p&gt;

&lt;p&gt;It is difficult to define exactly when to use &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, or &lt;code&gt;bind&lt;/code&gt;, as it will depend on the context of your program. &lt;code&gt;bind&lt;/code&gt; can be particularly helpful when you want to use events to access properties of one class within another class. For example, if you were to write a simple game, you might separate the user interface and I/O into one class, and the game logic and state into another. Since the game logic would need to access input, such as key press and click, you would want to &lt;code&gt;bind&lt;/code&gt; the events to access the &lt;code&gt;this&lt;/code&gt; value of the game logic class.&lt;/p&gt;

&lt;p&gt;The important part is to know how to determine what object &lt;code&gt;this&lt;/code&gt; refers to, which you can do implicitly with what you learned in the previous sections, or explicitly with the three methods you will learn next.&lt;/p&gt;

&lt;h3&gt;
  
  
  Call and Apply
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; are very similar—they invoke a function with a specified &lt;code&gt;this&lt;/code&gt; context, and optional arguments. The only difference between &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; is that &lt;code&gt;call&lt;/code&gt; requires the arguments to be passed in one-by-one, and &lt;code&gt;apply&lt;/code&gt; takes the arguments as an array.&lt;/p&gt;

&lt;p&gt;In this example, we’ll create an object, and create a function that references &lt;code&gt;this&lt;/code&gt; but has no &lt;code&gt;this&lt;/code&gt; context.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const book = {
  title: 'Brave New World',
  author: 'Aldous Huxley',
}

function summary() {
  console.log(`${this.title} was written by ${this.author}.`)
}

summary()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"undefined was written by undefined"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;summary&lt;/code&gt; and &lt;code&gt;book&lt;/code&gt; have no connection, invoking &lt;code&gt;summary&lt;/code&gt; by itself will only print &lt;code&gt;undefined&lt;/code&gt;, as it’s looking for those properties on the global object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Attempting this in strict mode would result in &lt;code&gt;Uncaught TypeError: Cannot read property 'title' of undefined&lt;/code&gt;, as &lt;code&gt;this&lt;/code&gt; itself would be &lt;code&gt;undefined&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;However, you can use &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; to invoke the &lt;code&gt;this&lt;/code&gt; context of &lt;code&gt;book&lt;/code&gt; on the function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;summary.call(book)
// or:
summary.apply(book)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"Brave New World was written by Aldous Huxley."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is now a connection between &lt;code&gt;book&lt;/code&gt; and &lt;code&gt;summary&lt;/code&gt; when these methods are applied. Let’s confirm exactly what &lt;code&gt;this&lt;/code&gt; is.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printThis() {
  console.log(this)
}

printThis.call(book)
// or:
whatIsThis.apply(book)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
{title: "Brave New World", author: "Aldous Huxley"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;this&lt;/code&gt; actually becomes the object passed as an argument.&lt;/p&gt;

&lt;p&gt;This is how &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; are the same, but there is one small difference. In addition to being able to pass the &lt;code&gt;this&lt;/code&gt; context as the first argument, you can also pass additional arguments through.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function longerSummary(genre, year) {
  console.log(
    `${this.title} was written by ${this.author}. It is a ${genre} novel written in ${year}.`
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With &lt;code&gt;call&lt;/code&gt; each additional value you want to pass is sent as an additional argument.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;longerSummary.call(book, 'dystopian', 1932)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"Brave New World was written by Aldous Huxley. It is a dystopian novel written in 1932."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to send the exact same arguments with &lt;code&gt;apply&lt;/code&gt;, this is what happens:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;longerSummary.apply(book, 'dystopian', 1932)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
Uncaught TypeError: CreateListFromArrayLike called on non-object at &amp;lt;anonymous&amp;gt;:1:15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, for &lt;code&gt;apply&lt;/code&gt;, you have to pass all the arguments in an array.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;longerSummary.apply(book, ['dystopian', 1932])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"Brave New World was written by Aldous Huxley. It is a dystopian novel written in 1932."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between passing the arguments individually or in an array is subtle, but it’s important to be aware of. It might be simpler and more convenient to use &lt;code&gt;apply&lt;/code&gt;, as it would not require changing the function call if some parameter details changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bind
&lt;/h3&gt;

&lt;p&gt;Both &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; are one-time use methods—if you call the method with the &lt;code&gt;this&lt;/code&gt; context it will have it, but the original function will remain unchanged.&lt;/p&gt;

&lt;p&gt;Sometimes, you might need to use a method over and over with the &lt;code&gt;this&lt;/code&gt; context of another object, and in that case you could use the &lt;code&gt;bind&lt;/code&gt; method to create a brand new function with an explicitly bound &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const braveNewWorldSummary = summary.bind(book)

braveNewWorldSummary()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output
"Brave New World was written by Aldous Huxley"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, every time you call &lt;code&gt;braveNewWorldSummary&lt;/code&gt;, it will always return the original &lt;code&gt;this&lt;/code&gt; value bound to it. Attempting to bind a new &lt;code&gt;this&lt;/code&gt; context to it will fail, so you can always trust a bound function to return the &lt;code&gt;this&lt;/code&gt; value you expect.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const braveNewWorldSummary = summary.bind(book)

braveNewWorldSummary() // Brave New World was written by Aldous Huxley.

const book2 = {
  title: '1984',
  author: 'George Orwell',
}

braveNewWorldSummary.bind(book2)

braveNewWorldSummary() // Brave New World was written by Aldous Huxley.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Although this example tries to bind &lt;code&gt;braveNewWorldSummary&lt;/code&gt; once again, it retains the original &lt;code&gt;this&lt;/code&gt; context from the first time it was bound.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arrow Functions
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-define-functions-in-javascript#arrow-functions" rel="noopener noreferrer"&gt;Arrow functions&lt;/a&gt; do not have their own &lt;code&gt;this&lt;/code&gt; binding. Instead, they go up to the next level of execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const whoAmI = {
  name: 'Leslie Knope',
  regularFunction: function() {
    console.log(this.name)
  },
  arrowFunction: () =&amp;gt; {
    console.log(this.name)
  },
}

whoAmI.regularFunction() // "Leslie Knope"
whoAmI.arrowFunction() // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It can be useful to use the arrow function in cases where you really want &lt;code&gt;this&lt;/code&gt; to refer to the outer context. For example, if you had an event listener inside of a class, you would probably want &lt;code&gt;this&lt;/code&gt; to refer to some value in the class.&lt;/p&gt;

&lt;p&gt;In this example, you’ll create and append button to the DOM like before, but the class will have an event listener that will change the text value of the button when clicked.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const button = document.createElement('button')
button.textContent = 'Click me'
document.body.append(button)

class Display {
  constructor() {
    this.buttonText = 'New text'

    button.addEventListener('click', event =&amp;gt; {
      event.target.textContent = this.buttonText
    })
  }
}

new Display()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you click the button, the text content will change to the value of &lt;code&gt;buttonText&lt;/code&gt;. If you hadn’t used an arrow function here, &lt;code&gt;this&lt;/code&gt; would be equal to &lt;code&gt;event.currentTarget&lt;/code&gt;, and you wouldn’t be able to use it to access a value within the class without explicitly binding it. This tactic is often used on class methods in frameworks like React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, you learned about &lt;code&gt;this&lt;/code&gt; in JavaScript, and the many different values it might have based on implicit runtime binding, and explicit binding through &lt;code&gt;bind&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt;, and &lt;code&gt;apply&lt;/code&gt;. You also learned about how the lack of &lt;code&gt;this&lt;/code&gt; binding in arrow functions can be used to refer to a different context. With this knowledge, you should be able to determine the value of &lt;code&gt;this&lt;/code&gt; in your programs.&lt;/p&gt;




&lt;p&gt;&lt;a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9dopc8jjbnew6l2xc2j.png" alt="CC 4.0 License"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This work is licensed under a &lt;a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener noreferrer"&gt;Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Understanding Bits, Bytes, Bases, and Writing a Hex Dump in JavaScript</title>
      <dc:creator>Tania Rascia</dc:creator>
      <pubDate>Thu, 30 May 2019 20:14:54 +0000</pubDate>
      <link>https://dev.to/taniarascia/understanding-bits-bytes-bases-and-writing-a-hex-dump-in-javascript-l6f</link>
      <guid>https://dev.to/taniarascia/understanding-bits-bytes-bases-and-writing-a-hex-dump-in-javascript-l6f</guid>
      <description>&lt;p&gt;I was recently tasked with creating a simple command line program that would take an input of a file of unknown contents and print a hex dump as the output. However, I didn't really know how I could access the data of the file to begin with, and I didn't know what a hex dump was. So I'm going to share with you what I learned and what I wrote to accomplish this task.&lt;/p&gt;

&lt;p&gt;Since I'm most familiar with JavaScript, I decided to do this in Node. The aim is to write a command like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;node hex.js data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which will run a &lt;code&gt;hex.js&lt;/code&gt; program on a file (&lt;code&gt;data&lt;/code&gt;) and output the hex dump. &lt;/p&gt;

&lt;p&gt;The file can be anything - an image, a binary, a regular text file, or a file with other encoded data. In my particular case, it was a ROM. &lt;/p&gt;

&lt;p&gt;If you've ever tried opening a non text-based file with a text editor, you'll remember seeing a jumbled mess of random characters. If you've ever wondered how a program could access that raw data and work with it, this article might be enlightening.&lt;/p&gt;

&lt;p&gt;This article will consist of two parts: the first, background information explaining what a hex dump is, what bits and bytes are, how to calculate values in base 2, base 10, and base 16, and an explanation of printable ASCII characters. The second part will be writing the hex dump function in Node.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's a Hex Dump?
&lt;/h2&gt;

&lt;p&gt;To understand what a hex dump is, we can create a file and view one. I'll make a simple text file consisting of a Bob Ross quote. (&lt;code&gt;-en&lt;/code&gt; here is preventing trailing newlines and allowing interpretation of backslash-escaped characters, which will come in handy in a bit.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-en&lt;/span&gt; &lt;span class="s2"&gt;"Just make a decision and let it go."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;data&lt;/code&gt; is just a filename, not any sort of command or keyword. &lt;/p&gt;

&lt;p&gt;Unix systems already have a &lt;a href="http://man7.org/linux/man-pages/man1/hexdump.1.html"&gt;hexdump command&lt;/a&gt;, and I'll use the canonical (&lt;code&gt;-C&lt;/code&gt;) flag to format the output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;hexdump &lt;span class="nt"&gt;-C&lt;/span&gt; data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here's what I get.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000  4a 75 73 74 20 6d 61 6b  65 20 61 20 64 65 63 69  |Just make a deci|
00000010  73 69 6f 6e 20 61 6e 64  20 6c 65 74 20 69 74 20  |sion and let it |
00000020  67 6f 2e                                          |go.|
00000023
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay, so I have a bunch of numbers, and on the right we can see the text characters from the string I just echoed. The man page tells us that &lt;code&gt;hexdump&lt;/code&gt; "displays file contents in hexadecimal, decimal, octal, or ascii". The specific format used here (canonical) is further explained:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;-C, --canonical&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Canonical hex+ASCII display.  Display the input offset in&lt;br&gt;
 hexadecimal, followed by sixteen space-separated, two-column,&lt;br&gt;
 hexadecimal bytes, followed by the same sixteen bytes in &lt;strong&gt;%_p&lt;/strong&gt;&lt;br&gt;
 format enclosed in '&lt;strong&gt;|&lt;/strong&gt;' characters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So now we can see that each line is a hexadecimal input offset (address) which is kind of like a line number, followed by 16 hexadecimal bytes, followed by the same bytes in ASCII format between two pipes. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Address&lt;/th&gt;
&lt;th&gt;Hexadecimal bytes&lt;/th&gt;
&lt;th&gt;ASCII&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;00000000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;4a 75 73 74 20 6d 61 6b  65 20 61 20 64 65 63 69&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt; |Just make a deci|&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;00000010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;73 69 6f 6e 20 61 6e 64  20 6c 65 74 20 69 74 20&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;|sion and let it|&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;00000020&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;67 6f 2e&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;|go.|&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;00000023&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This kind of makes sense for viewing ASCII text, but what about data that can't be represented by ASCII? How will that look? In this example, I'll echo 0-15 represented in base 16/hexidecimal, which will be &lt;code&gt;00&lt;/code&gt; to &lt;code&gt;0f&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-en&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;00&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;01&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;02&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;03&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;04&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;05&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;06&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;07&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;08&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;09&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;0a&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;0b&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;0c&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;0d&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;0e&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;0f"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; data2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These numbers don't correspond to any ASCII characters, and also cannot be viewed in a regular text editor. If you try opening it in VSCode, for example, you'll see "The file is not displayed in the editor because it is either binary or uses an unsupported text encoding.". &lt;/p&gt;

&lt;p&gt;If you do decide to open it anyway, you'll probably see what appears to be a question mark. Fortunately, we can view the raw contents with hexdump.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
00000010
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, unprintable ASCII characters are represented by a &lt;code&gt;.&lt;/code&gt;, and the bytes are confirmed hexadecimal. The address has &lt;code&gt;10&lt;/code&gt; on the second line because it's starting on the 16th byte, and 16 is &lt;code&gt;10&lt;/code&gt; in hexadecimal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Bytes and Bases
&lt;/h2&gt;

&lt;p&gt;Looking at the "hexadecimal bytes" section of the &lt;code&gt;hexdump&lt;/code&gt; table, you have to know what both "hexadecimal" means, and what "bytes" are. &lt;/p&gt;

&lt;p&gt;You probably already know that a kilobyte is roughly a thousand bytes, or &lt;code&gt;1024&lt;/code&gt; bytes, and a megabyte is roughly a thousand kilobytes, or &lt;code&gt;1,024 * 1,024&lt;/code&gt; bytes (&lt;code&gt;1,048,576&lt;/code&gt; bytes), or maybe even that a floppy disk has &lt;code&gt;1,474,560&lt;/code&gt; bytes of storage.&lt;/p&gt;

&lt;p&gt;But what exactly is a byte? &lt;/p&gt;

&lt;h3&gt;
  
  
  Bits, nibbles, and bytes
&lt;/h3&gt;

&lt;p&gt;A bit is a binary digit, the smallest form of data on a computer, and can be &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;. Like a Boolean, a bit can represent on/off, true/false, etc. There are four bits in a nibble, and eight bits in a byte.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Unit&lt;/th&gt;
&lt;th&gt;Storage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bit&lt;/td&gt;
&lt;td&gt;Binary digit (&lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nibble&lt;/td&gt;
&lt;td&gt;4 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Byte&lt;/td&gt;
&lt;td&gt;8 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Computers manipulate data in bytes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Value of a byte
&lt;/h3&gt;

&lt;p&gt;Did you ever play a video game that maxed out the quantity of an item in your inventory at &lt;code&gt;255&lt;/code&gt;? Why did it stop at that point?&lt;/p&gt;

&lt;p&gt;If each inventory unit was one byte, then what's the highest value that can be represented?&lt;/p&gt;

&lt;p&gt;This is easy to see in binary (base 2). For a byte, there are 8 1-bit slots. The highest value of a bit is &lt;code&gt;1&lt;/code&gt;, so the highest binary 8-bit value is 8 &lt;code&gt;1&lt;/code&gt;s. &lt;/p&gt;

&lt;h4&gt;
  
  
  Binary: 111111112
&lt;/h4&gt;

&lt;p&gt;How do you know &lt;code&gt;11111111&lt;/code&gt; represents the number &lt;code&gt;255&lt;/code&gt; (in decimal)? Starting from the least significant value (the one all the way to the right), you'll multiply the digit by the result of the base raised to its position, and add them all together.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1 * 2**7&lt;/code&gt; + &lt;code&gt;1 * 2**6&lt;/code&gt; + &lt;code&gt;1 * 2**5&lt;/code&gt; + &lt;code&gt;1 * 2**4&lt;/code&gt; + &lt;code&gt;1 * 2**3&lt;/code&gt; + &lt;code&gt;1 * 2**2&lt;/code&gt; + &lt;code&gt;1 * 2**1&lt;/code&gt; + &lt;code&gt;1 * 2**0&lt;/code&gt; = &lt;code&gt;255&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Decimal: 25510
&lt;/h4&gt;

&lt;p&gt;If that doesn't make sense, think about it in decimal. For example, you know &lt;code&gt;007&lt;/code&gt; and &lt;code&gt;070&lt;/code&gt; and &lt;code&gt;700&lt;/code&gt; are all very different values (leading zeros have no effect on the value). Seven is &lt;code&gt;7 * 10^0&lt;/code&gt;, seventy is &lt;code&gt;7 * 10^1&lt;/code&gt;, and seven hundred is &lt;code&gt;7 * 10^2&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Number&lt;/th&gt;
&lt;th&gt;Decimal Represenation&lt;/th&gt;
&lt;th&gt;Calculation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Seven&lt;/td&gt;
&lt;td&gt;&lt;code&gt;007&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;7 * 10^0&lt;/code&gt; or &lt;code&gt;7 * 1&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Seventy&lt;/td&gt;
&lt;td&gt;&lt;code&gt;070&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;7 * 10^1&lt;/code&gt; or &lt;code&gt;7 * 10&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Seven hundred&lt;/td&gt;
&lt;td&gt;&lt;code&gt;700&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;7 * 10^2&lt;/code&gt; or &lt;code&gt;7 * 100&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So as we can see, the position of the digit determines the value, and we can use the same calulation to get &lt;code&gt;255&lt;/code&gt; in decimal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2 * 10**2&lt;/code&gt; + &lt;code&gt;5 * 10**1&lt;/code&gt; + &lt;code&gt;5 * 10**0&lt;/code&gt; = &lt;code&gt;255&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Hexadecimal: FF16
&lt;/h4&gt;

&lt;p&gt;This concept applies to any base. Hexadecimal is base 16, and &lt;code&gt;F&lt;/code&gt; represents the largest value, &lt;code&gt;15&lt;/code&gt; (&lt;code&gt;0&lt;/code&gt; is a value).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;15 * 16**1&lt;/code&gt; + &lt;code&gt;15 * 16**0&lt;/code&gt; = &lt;code&gt;255&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The same number
&lt;/h4&gt;

&lt;p&gt;So &lt;code&gt;11111111&lt;/code&gt;, &lt;code&gt;255&lt;/code&gt;, and &lt;code&gt;FF&lt;/code&gt; all represent the same number, which also happens to be the largest value of a byte. Hexadecimal is a convenient, compact way to represent the value of a byte, as it's always contained in two characters.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Number&lt;/th&gt;
&lt;th&gt;Base&lt;/th&gt;
&lt;th&gt;Calculation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1111111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Binary&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;1 * 2**7&lt;/code&gt; + &lt;code&gt;1 * 2**6&lt;/code&gt; + &lt;code&gt;1 * 2**5&lt;/code&gt; + &lt;code&gt;1 * 2**4&lt;/code&gt; + &lt;code&gt;1 * 2**3&lt;/code&gt; + &lt;code&gt;1 * 2**2&lt;/code&gt; + &lt;code&gt;1 * 2**1&lt;/code&gt; + &lt;code&gt;1 * 2**0&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;255&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Decimal&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;2 * 10**2&lt;/code&gt; + &lt;code&gt;5 * 10**1&lt;/code&gt; + &lt;code&gt;5 * 10**0&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;FF&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Hexadecimal&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;2 * 10**2&lt;/code&gt; + &lt;code&gt;5 * 10**1&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Representing other bases
&lt;/h3&gt;

&lt;p&gt;Programming languages will use a prefix to represent a value outside of base 10. Binary is &lt;code&gt;0b&lt;/code&gt;, and hexadecimal is &lt;code&gt;0x&lt;/code&gt;, so you can write &lt;code&gt;0b1111&lt;/code&gt; or &lt;code&gt;0xff&lt;/code&gt; in a Node repl, for example, and it will output the value in decimal.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Base&lt;/th&gt;
&lt;th&gt;Prefix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Binary&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0b&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hexadecimal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Counting in different bases
&lt;/h3&gt;

&lt;p&gt;The maximum value of a byte is &lt;code&gt;255&lt;/code&gt;, and the maximum value of a nibble (4 bits) is &lt;code&gt;15&lt;/code&gt;. Here's a chart counting to &lt;code&gt;15&lt;/code&gt; in binary, decimal, and hexadecimal. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Binary&lt;br&gt; (base 2)&lt;/th&gt;
&lt;th&gt;Decimal&lt;br&gt; (base 10)&lt;/th&gt;
&lt;th&gt;Hexadecimal&lt;br&gt; (base 16)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;01&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;02&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;03&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;04&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0101&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;05&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0110&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;6&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;06&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;7&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;07&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;08&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;9&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;09&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0a&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;11&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0b&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;12&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0c&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1101&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;13&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0d&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1110&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;14&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0e&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;15&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0f&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Just like in decimal, leading zeroes in any base don't affect the value, but hexadecimal is often written with leading zeroes, making the representation of a byte always two characters.&lt;/p&gt;

&lt;p&gt;So now we should have a good idea of the values represented in the address and bytes of a hex dump.&lt;/p&gt;

&lt;h3&gt;
  
  
  Printable ASCII characters
&lt;/h3&gt;

&lt;p&gt;Between &lt;code&gt;0x20&lt;/code&gt; and &lt;code&gt;0x7e&lt;/code&gt; are all the printable ASCII characters. &lt;a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters"&gt;This chart&lt;/a&gt; shows them all, along with their binary, octal, decimal, and hex counterparts. In the &lt;code&gt;hexdump&lt;/code&gt; example above, I printed &lt;code&gt;0x00&lt;/code&gt; to &lt;code&gt;0x0f&lt;/code&gt;, and since none of those are represented in ASCII, they appear as dots.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing a Hex Dump in JavaScript
&lt;/h2&gt;

&lt;p&gt;Now back to the original task of writing a hex dump program in Node. We know what it's supposed to look like, and we understand the values of the raw data, but where to start?&lt;/p&gt;

&lt;p&gt;Well, we know how we want the program to function. It should be able to use the filename as an argument and &lt;code&gt;console.log&lt;/code&gt; the hex dump.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;node hex.js data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So obviously I'll make &lt;code&gt;hex.js&lt;/code&gt; and I'll also make some new data that has both ASCII and non-ASCII representable data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-en&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;blink&amp;gt;Talent is pursued interest&amp;lt;/blink&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;00&lt;/span&gt;&lt;span class="se"&gt;\x&lt;/span&gt;&lt;span class="s2"&gt;ff"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And the goal is to make this output:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```00000000  3c 62 6c 69 6e 6b 3e 54  61 6c 65 6e 74 20 69 73  |Talent is|&lt;br&gt;
00000010  20 70 75 72 73 75 65 64  20 69 6e 74 65 72 65 73  | pursued interes|&lt;br&gt;
00000020  74 3c 2f 62 6c 69 6e 6b  3e 00 ff                 |t..|&lt;br&gt;
0000002b&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


### Getting a raw data buffer of a file

The first step is to obtain the data from the file somehow. I'll start by using the [file system module](https://nodejs.org/api/fs.html#fs_file_system).



```js
const fs = require('fs')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And to get the filename, we'll just get the 3rd command line argument (&lt;code&gt;0&lt;/code&gt; being the Node binary, &lt;code&gt;1&lt;/code&gt; being &lt;code&gt;hex.js&lt;/code&gt;, and &lt;code&gt;2&lt;/code&gt; being &lt;code&gt;data&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'll use &lt;a href="https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback"&gt;&lt;code&gt;readFile()&lt;/code&gt;&lt;/a&gt; to get the contents of the file. (&lt;code&gt;readFileSync()&lt;/code&gt; is just the synchronous version.) As the API says, "If no encoding is specified, then the raw buffer is returned", so we're getting a buffer. (&lt;code&gt;utf8&lt;/code&gt; is what we'd use for a string.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;hexdump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hexdump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will log out a &lt;code&gt;&amp;lt;Buffer&amp;gt;&lt;/code&gt; object (values removed for brevity).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Buffer 3c 62 6c 69 6e 6b 3e 54 ... 69 6e 6b 3e 00 ff&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay, this looks familiar. Thanks to all that background knowlege, we can see that the buffer is a bunch of bytes represented in hexadecimal. You can even see that final &lt;code&gt;00&lt;/code&gt; and &lt;code&gt;ff&lt;/code&gt; I echoed in there.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with a buffer
&lt;/h3&gt;

&lt;p&gt;You can treat the buffer like an array. If you check the length with &lt;code&gt;buffer.length&lt;/code&gt;, you'll get &lt;code&gt;43&lt;/code&gt;, which corresponds to the number of bytes. Since we want lines of 16 bytes, we can loop through every 16 and slice them into blocks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;hexdump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// cut buffer into blocks of 16&lt;/span&gt;

    &lt;span class="nx"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;lines&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we have an array of smaller buffers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
[ &amp;lt;Buffer 3c 62 6c 69 6e 6b 3e 54 61 6c 65 6e 74 20 69 73&amp;gt;,
  &amp;lt;Buffer 20 70 75 72 73 75 65 64 20 69 6e 74 65 72 65 73&amp;gt;,
  &amp;lt;Buffer 74 3c 2f 62 6c 69 6e 6b 3e 00 ff&amp;gt; ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Calculating the address
&lt;/h3&gt;

&lt;p&gt;We want to represent the address in hexadecimal, and you can convert a number to a hex string with &lt;code&gt;toString(16)&lt;/code&gt;. Then I'll just prepend some zeroes so it's always the same length.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So what would happen if I put the address and block in a template string?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;block&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ '00000000 &amp;lt;blink&amp;gt;Talent is',
  '00000010  pursued interes',
  '00000020 t&amp;lt;/blink&amp;gt;\u0000�' ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The template tries to convert the buffer to a string. It doesn't interpret the non-ASCII characters the way we want though, so we won't be able to do that for the ASCII output. We have the correct addresses now, though.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating hex and ASCII strings
&lt;/h3&gt;

&lt;p&gt;When you access each value in a buffer, it interprets it as the raw number, whether you choose to represent it as binary, hex, ASCII, or anything else is up to you. I'm going to make an array for hex and an array for ASCII, then join them into strings. This way the template literal will already have a string representation to work with.&lt;/p&gt;

&lt;p&gt;In order to get the ASCII characters, we can test the value based on the printable ASCII chart above -  &lt;code&gt;&amp;gt;= 0x20&lt;/code&gt; and &lt;code&gt;&amp;lt; 0x7f&lt;/code&gt; - then get the character code or a dot. Getting the hex values is the same as the address - convert it to a base 16 string and pad single values with a &lt;code&gt;0&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I'll add some space to the line and convert the lines to newline-separated strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;hexdump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// address&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// cut buffer into blocks of 16&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hexArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;asciiArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="nx"&gt;asciiArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x20&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mh"&gt;0x7f&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hexString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;asciiString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;asciiArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;hexString&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;  |&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;asciiString&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;|`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we're almost there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000 3c 62 6c 69 6e 6b 3e 54 61 6c 65 6e 74 20 69 73 |&amp;lt;blink&amp;gt;Talent is|
00000010 20 70 75 72 73 75 65 64 20 69 6e 74 65 72 65 73 | pursued interes|
00000020 74 3c 2f 62 6c 69 6e 6b 3e 00 ff |t&amp;lt;/blink&amp;gt;..|
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Full hex dump program
&lt;/h3&gt;

&lt;p&gt;The only thing that remains at this point is some final formatting - adding padding to the last line if has less than 16 bytes, and separating the bytes into two blocks of eight, which isn't too important for me to explain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/taniarascia/7ff2e83577d83b85a421ab36ab2ced84"&gt;Here's a gist&lt;/a&gt; of the final version, or see below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;hexdump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// address&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// cut buffer into blocks of 16&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hexArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;asciiArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;padding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="nx"&gt;asciiArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x20&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mh"&gt;0x7f&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// if block is less than 16 bytes, calculate remaining space&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;space&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
      &lt;span class="nx"&gt;padding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;space&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;space&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// calculate extra space if 8 or less&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hexString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;  &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;hexArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;asciiString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;asciiArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;hexString&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;|&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;asciiString&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;|`&lt;/span&gt;

    &lt;span class="nx"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hexdump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I covered a lot of concepts in this article.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bits, nibbles, and bytes&lt;/li&gt;
&lt;li&gt;Binary, decimal, and hexadecimal numbers&lt;/li&gt;
&lt;li&gt;Calculating the value of a number in any base system&lt;/li&gt;
&lt;li&gt;Printable ASCII characters&lt;/li&gt;
&lt;li&gt;Accessing file data in Node.js&lt;/li&gt;
&lt;li&gt;Working with buffers of raw data&lt;/li&gt;
&lt;li&gt;Converting numbers to hex and ASCII&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is still more I want to write about on this subject, such as creating a 16-bit hex dump, bitwise operators, and endianness, as well as using Streams to improve this hex dump function, so probably more to come in a follow up article.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
