3 Different Ways to Create Functions in Javascript

As a beginner to JS, one thing that confused me when I looked at others’ code was their use of different function syntaxes. To illustrate, here is the syntax I originally learned. This is a simple function that adds two numbers:

function add(num1, num2) {
	return num1 + num2;
}

Here is that same function with ES5 syntax. Note that this method assigns the function to a variable. You can then use that variable as a normal function:

var add = function(num1, num2) {
	return num1 + num2;
};

Also notice the semicolon after the statement. That is there because it is acting as a regular variable declaration statement.

Finally, here is the latest syntax as defined in the ES6 syntax:

const add = (num1, num2) => {
	return num1 + num2
};

However, if you only had one expression, the curly brackets aren’t required, like so:

const add = (num1, num2) => return num1 + num2;

In addition, if there is only one argument, you can dispose with the parentheses. This function returns the square of a variable:

const square = var => return var^2;

However, if there are no variables, the parenthesis are needed:

const hello_world = () => console.log('Hello World');

Conclusion

Using these different syntax’s for functions can make your functions more concise, in addition to making your code more up-to-date.

How to Install Plugins in Vim using VimPlug

Recently, I started using Vim as my default code editor, instead of Textmate.

After gaining a reasonable proficiency in it, I started exploring other things that could be done in it.

It turns out that Vim can be extended to do almost anything you want. If a solution for your problem hasn’t already been made, you can make one yourself.

However, I could not find a guide to installing plugins in Vim. Eventually, after some trial and error, (and looking at others’ ~/.vimrc’s) I figured out a way. Here it is:

Install VimPlug (taken from VimPlug’s Github page):

If you’re on MacOS or Linux, go to the Terminal and then run:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

If you are on Windows, open cmd or Command Prompt and run:

md ~\vimfiles\autoload
$uri = 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
(New-Object Net.WebClient).DownloadFile(
  $uri,
 $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
    "~\vimfiles\autoload\plug.vim"
  )
)

These commands simply download plug.vim and copy it to vim’s “autoload” directory.

Now, set up your ~/.vimrc:

In your Terminal (or Command Prompt) enter:

vim ~/.vimrc

Then, at the top of the file, enter these lines (I’m assuming you have at least a basic knowledge of using Vim. If not, check out Vim 101):

" VimPlug setup  
call plug#begin('~/.vim/plugged')

 " Enter plugins here`

 call plug#end()

Next, find the plugins you want and specify them in ~/.vimrc:

The easiest way to find vim plugins is to browse Vim Awesome. I chose Airline, a utility that draws a nice status line at the bottom of my vim window.

Click on “VimPlug” as shown:

In your Terminal (or Command Prompt), type:

vim ~/.vimrc

Replace the line that says  "Enter plugins here, with the line that VimAwesome gave you. In this case, it was Plug 'bling/vim-airline'. Make sure to save with :w.

Finally, install it!

In Vim, type :PlugInstall.

If all went well, you should see Finishing … Done!

Congratulations! You now know how to install VimPlugins. Just place any new plugins you find inside the lines in your ~/.vimrc where you call VimPlug and then run :PlugInstall to finish.

How to Install Applications on macOS using Homebrew

This tutorial teaches you how to install applications through Homebrew, an unofficial package manager for macOS. It assumes that you have already installed Homebrew on your Mac. If not, check out our instruction guide.

For instance, to install MacVim, instead of downloading the application from MacVim’s website, simply go to Brew Formulas, a website that lists all of the applications available through Homebrew. After searching  for MacVim on there, simply run the command they give, brew install macvim in the Terminal. Now MacVim is installed!

Benefits

One major benefit that Homebrew gives over simply downloading the package manually is automatic updates. To update all of the applications that you have installed via Homebrew, simply run brew update && brew upgrade.

Straightforward Guide to Installing Homebrew on macOS

Requirements

  • macOS 10.11 or higher
  • xCode Command Line Tools (if you don’t have these already, either download xCode from the App Store or run xcode-select --install in your Terminal)

The Process

Installing Homebrew is an easy one-step process. Just run:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"` in your Terminal and press Enter.

Verification

To make sure that Homebrew is installed, run

brew doctor

If it returns Your system is ready to brew, then you’re good to go!

Now check out our article about Installing Applications on Your Mac Using Homebrew.

Straightforward Guide to Installing NodeJS

Table of Contents

Mac

First of all, you need to install xCode and Homebrew. While there is technically a way to install Node without using them, using Homebrew is safer and easier. (If you want to investigate this alternative method, check out the NodeJS documentation.)

To install xCode, go to the Mac App Store, search for ‘xCode,’ click on ‘Get’ and then ‘Install app.’ It’s a rather large download, to the tune of 5.3 GB, so go read a book while waiting for it.

Once it’s done, open xCode and accept any Terms and Conditions. Once it gives you an option start a new projectgo to the Terminal application and run this command to download Homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Now, use Homebrew to install NodeJS:

brew install node

Once that’s done, check and ensure that it’s installed correctly. Run node -v & npm -v. If they both return a version number, then you’re done! NodeJS has been installed.

Windows

The Windows installation is relatively straightforward. Just go to the NodeJS website and download the Windows installer. Once you have it on your computer, double-click on the installer and follow the prompts.

Once it finishes, simply restart your computer. Voila!

Linux

If you’re using Linux, you are probably familiar with the concept of running commands in the Terminal. All you need to do is take these commands and run them one by one. Just press Continue whenever it prompts you.

  • If you have an Ubuntu- or Debian-based distribution, run:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential
  • If you have a Fedora-based distribution, run:
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum -y install nodejs
sudo yum install gcc-c++ make
  • Finally, if you have an Arch-based distribution, run:
pacman -S nodejs npm

Conclusion

There you have it. You have successfully installed both NodeJS and its package manager, NPM. To read more about installing Node on these and other operating systems, check out the official NodeJS documentation.