Skip to content

Instantly share code, notes, and snippets.

Simply because of [Automatic Semicolon Insertion (ASI)]. Because of ASI, for example if we put the return value (the thing to return) on the next line of the return keyword, nothing is actually returned. Example:

function provideGreetingSameLine() {
  return 'hello';
}

// Prints "hello".
console.log(provideGreetingSameLine());
@ugultopu
ugultopu / How GitHub sets the default branch of a repository?.md
Created December 16, 2020 11:58
Explanation of how GitHub initially sets the default branch of a repository

On a repository that already contains multiple branches, you can set (change) the default branch for that repository by:

  • Navigating to the repository on GitHub.
  • Then to "Settings -> Branches".
  • Then by changing the value of the "Default branch" setting.

However, on a new repository, there isn't a default branch yet because there aren't any branches yet! GitHub sets the default branch on the very first push to that repository. It does it as follows:

  • In the very first push of the repository to GitHub, try to find a branch that has the same name as the value of the "Repository default branch" setting. (The "Repository default branch" setting is accessed by clicking on your user avatar on GitHub, then "Settings -> Repositories".)
  • If the very first push contains a branch whose name is the same as the value of the "Repository default branch" setting, then GitHub sets that branch as the default branch for that repository. Otherwise, the default branch becomes the branch whose name is alphabetically the s
@ugultopu
ugultopu / Stack traces in JavaScript.md
Created December 20, 2020 17:18
Information about the nature and formatting of the stack traces in JavaScript, using Node.js for the examples

When you see a stack trace on your terminal, it is not some special, magical thing. It is simply nothing but a string representation of an Error instance. That is, a stack trace is nothing but the output of the toString method of that Error instance.

When you create an Error instance, an object is created with the following properties which has the following attributes. Put the following in a file named script.js and run it with node script.js:

const e = Error('Some optional message');
console.log(getAllProperties(e));

// This function is from https://gist.github.com/ugultopu/15abb1790b3d08384a372cdef3ab5b81
function getAllProperties(object) {
@ugultopu
ugultopu / GitHub Pages Flowchart.md
Created December 20, 2020 18:54
A step-by-step explanation of how GitHub Pages work
  • Create a repository on GitHub.

  • Make your very first push to the repository.

  • Did you name your repository "username.github.io", where username is your GitHub username?

  • If yes, then on your very first push to the repository, GitHub Pages is automatically enabled to be built from the root directory of the default branch. You can read more about how the default branch is set here. Note that the default branch is determined according to the rules described in that link and that's the branch that GitHub Pages will build the web site from. Having a branch named gh-pages on your very first push does not matter. That is, even if you have a branch named gh-pages on your very first push, it will not be treated as a "special" branch. It will be treated just like any other branch. The default branch will be determined according to the rules explained in the link, and GitHub Pages will be build your web site from the root directory of

@ugultopu
ugultopu / How to preserve the original stack trace in callbacks?.md
Last active December 22, 2020 17:22
A description of the problem of and potential solutions for understanding the location where a callback is used at

Summary

Use the following pattern in your callbacks:

require('fs').writeFile(
  'some_non_existent_directory/some_file',
  '',
  err => throwIfError(err),
);
@ugultopu
ugultopu / What is a function "statement" in JavaScript?.md
Last active December 26, 2020 23:52
Semantics of defining a function within a block in JavaScript

UPDATE

Apparently, "function statements" (that is, "block-level function declarations") have been standardized in ES6 (ES2015). This question contains more information about their behavior. However, before ES6 (that is, on ES5 and before), they are not standard. Read the following for more information.

Original Text

In JavaScript, we have function declarations:

function someFunction(/* Some parameters */) {
 // Some code
@ugultopu
ugultopu / There is no block scope in JavaScript, there is only function scope.md
Created December 27, 2020 00:25
Explaining the scope of variables in JavaScript

Well at least there wasn't. Before ES6 (ES2015), the only way to declare a variable was to use the var keyword. And a var is always scoped to the function in which it is defined. Before ES6, there was no such thing as "block scope" in JavaScript. All variables (and function declarations/expressions) were function scoped. That is, for example the following:

function someFunction() {
  for (var i = 0; i < 10; i++) {
    // Some operation
  }
}
@ugultopu
ugultopu / async-await is nothing but syntactic sugar for promises.md
Created December 27, 2020 22:05
async/await is just a convenient way of creating and using promises

That's it. async/await is not yet another technology for asynchronous programming in JavaScript. It is simply a wrapper, a syntactic sugar for promise creation. Quoting this post:

async/await is syntactic sugar around promise creation and consumption. It's really helpful sugar, but that's all it is. async functions return promises, await expressions are the equivalent of then callbacks (of promises), and try/catch around await expressions is the equivalent of a catch callback (of promises).

The question "Is async/await just syntactic sugar for promises?" came to my mind when I was reading the API documentation for the fs module of Node.js. In the documentation, there were two versions of every function (two versions when we exclude the "sync" versions). One of them was the good old callback based one and the other one was a new promise based one. Not seeing an async/await based

Summary

If your callback is in the form:

err => {
  if (err) callback(err);
  else callback();
}