One place for hosting & domains

      How To Write and Run Your First Program in Node.js


      The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      Node.js is a popular open-source runtime environment that can execute JavaScript outside of the browser using the V8 JavaScript engine, which is the same engine used to power the Google Chrome web browser’s JavaScript execution. The Node runtime is commonly used to create command line tools and web servers.

      Learning Node.js will allow you to write your front-end code and your back-end code in the same language. Using JavaScript throughout your entire stack can help reduce time for context switching, and libraries are more easily shared between your back-end server and front-end projects.

      Also, thanks to its support for asynchronous execution, Node.js excels at I/O-intensive tasks, which is what makes it so suitable for the web. Real-time applications, like video streaming, or applications that continuously send and receive data, can run more efficiently when written in Node.js.

      In this tutorial you’ll create your first program with the Node.js runtime. You’ll be introduced to a few Node-specific concepts and build your way up to create a program that helps users inspect environment variables on their system. To do this, you’ll learn how to output strings to the console, receive input from the user, and access environment variables.

      Prerequisites

      To complete this tutorial, you will need:

      Step 1 — Outputting to the Console

      To write a “Hello, World!” program, open up a command line text editor such as nano and create a new file:

      With the text editor opened, enter the following code:

      hello.js

      console.log("Hello World");
      

      The console object in Node.js provides simple methods to write to stdout, stderr, or to any other Node.js stream, which in most cases is the command line. The log method prints to the stdout stream, so you can see it in your console.

      In the context of Node.js, streams are objects that can either receive data, like the stdout stream, or objects that can output data, like a network socket or a file. In the case of the stdout and stderr streams, any data sent to them will then be shown in the console. One of the great things about streams is that they're easily redirected, in which case you can redirect the output of your program to a file, for example.

      Save and exit nano by pressing CTRL+X, when prompted to save the file, press Y. Now your program is ready to run.

      Step 2 — Running the Program

      To run this program, use the node command as follows:

      The hello.js program will execute and display the following output:

      Output

      Hello World

      The Node.js interpreter read the file and executed console.log("Hello World"); by calling the log method of the global console object. The string "Hello World" was passed as an argument to the log function.

      Although quotation marks are necessary in the code to indicate that the text is a string, they are not printed to the screen.

      Having confirmed that the program works, let's make it more interactive.

      Step 3 — Receiving User Input via Command Line Arguments

      Every time you run the Node.js “Hello, World!” program, it produces the same output. In order to make the program more dynamic, let's get input from the user and display it on the screen.

      Command line tools often accept various arguments that modify their behavior. For example, running node with the --version argument prints the installed version instead of running the interpreter. In this step, you will make your code accept user input via command line arguments.

      Create a new file arguments.js with nano:

      Enter the following code:

      arguments.js

      console.log(process.argv);
      

      The process object is a global Node.js object that contains functions and data all related to the currently running Node.js process. The argv property is an array of strings containing all the command line arguments given to a program.

      Save and exit nano by typing CTRL+X, when prompted to save the file, press Y.

      Now when you run this program, you provide a command line argument like this:

      • node arguments.js hello world

      The output looks like the following:

      Output

      [ '/usr/bin/node', '/home/sammy/first-program/arguments.js', 'hello', 'world' ]

      The first argument in the process.argv array is always the location of the Node.js binary that is running the program. The second argument is always the location of the file being run. The remaining arguments are what the user entered, in this case: hello and world.

      We are mostly interested in the arguments that the user entered, not the default ones that Node.js provides. Open the arguments.js file for editing:

      Change console.log(process.arg); to the following:

      arguments.js

      console.log(process.argv.slice(2));
      

      Because argv is an array, you can use JavaScript's built-in slice method that returns a selection of elements. When you provide the slice function with 2 as its argument, you get all the elements of argv that comes after its second element; that is, the arguments the user entered.

      Re-run the program with the node command and the same arguments as last time:

      • node arguments.js hello world

      Now, the output looks like this:

      Output

      [ 'hello', 'world' ]

      Now that you can collect input from the user, let's collect input from the program's environment.

      Step 4 — Accessing Environment Variables

      Environment variables are key-value data stored outside of a program and provided by the OS. They are typically set by the system or user and are available to all running processes for configuration or state purposes. You can use Node's process object to access them.

      Use nano to create a new file environment.js:

      Add the following code:

      environment.js

      console.log(process.env);
      

      The env object stores all the environment variables that are available when Node.js is running the program.

      Save and exit like before, and run the environment.js file with the node command.

      Upon running the program, you should see output similar to the following:

      Output

      { SHELL: '/bin/bash', SESSION_MANAGER: 'local/digitalocean:@/tmp/.ICE-unix/1003,unix/digitalocean:/tmp/.ICE-unix/1003', COLORTERM: 'truecolor', SSH_AUTH_SOCK: '/run/user/1000/keyring/ssh', XMODIFIERS: '@im=ibus', DESKTOP_SESSION: 'ubuntu', SSH_AGENT_PID: '1150', PWD: '/home/sammy/first-program', LOGNAME: 'sammy', GPG_AGENT_INFO: '/run/user/1000/gnupg/S.gpg-agent:0:1', GJS_DEBUG_TOPICS: 'JS ERROR;JS LOG', WINDOWPATH: '2', HOME: '/home/sammy', USERNAME: 'sammy', IM_CONFIG_PHASE: '2', LANG: 'en_US.UTF-8', VTE_VERSION: '5601', CLUTTER_IM_MODULE: 'xim', GJS_DEBUG_OUTPUT: 'stderr', LESSCLOSE: '/usr/bin/lesspipe %s %s', TERM: 'xterm-256color', LESSOPEN: '| /usr/bin/lesspipe %s', USER: 'sammy', DISPLAY: ':0', SHLVL: '1', PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin', DBUS_SESSION_BUS_ADDRESS: 'unix:path=/run/user/1000/bus', _: '/usr/bin/node', OLDPWD: '/home/sammy' }

      Keep in mind that many of the environment variables you see are dependent on the configuration and settings of your system, and your output may look substantially different than what you see here. Rather than viewing a long list of environment variables, you might want to retrieve a specific one.

      Step 5 — Accessing a Specified Environment Variable

      In this step you'll view environment variables and their values using the global process.env object and print their values to the console.

      The process.env object is a simple mapping between environment variable names and their values stored as strings. Like all objects in JavaScript, you access an individual property by referencing its name in square brackets.

      Open the environment.js file for editing:

      Change console.log(process.env); to:

      environment.js

      console.log(process.env["HOME"]);
      

      Save the file and exit. Now run the environment.js program:

      The output now looks like this:

      Output

      /home/sammy

      Instead of printing the entire object, you now only print the HOME property of process.env, which stores the value of the $HOME environment variable.

      Again, keep in mind that the output from this code will likely be different than what you see here because it is specific to your system. Now that you can specify the environment variable to retrieve, you can enhance your program by asking the user for the variable they want to see.

      Step 6 — Retrieving An Argument in Response to User Input

      Next, you'll use the ability to read command line arguments and environment variables to create a command line utility that prints the value of an environment variable to the screen.

      Use nano to create a new file echo.js:

      Add the following code:

      echo.js

      const args = process.argv.slice(2);
      console.log(process.env[args[0]]);
      

      The first line of echo.js stores all the command line arguments that the user provided into a constant variable called args. The second line prints the environment variable stored in the first element of args; that is, the first command line argument the user provided.

      Save and exit nano, then run the program as follows:

      Now, the output would be:

      Output

      /home/sammy

      The argument HOME was saved to the args array, which was then used to find its value in the environment via the process.env object.

      At this point you can now access the value of any environment variable on your system. To verify this, try viewing the following variables: PWD, USER, PATH.

      Retrieving single variables is good, but letting the user specify how many variables they want would be better.

      Step 7 — Viewing Multiple Environment Variables

      Currently, the application can only inspect one environment variable at a time. It would be useful if we could accept multiple command line arguments and get their corresponding value in the environment. Use nano to edit echo.js:

      Edit the file so that it has the following code instead:

      echo.js

      const args = process.argv.slice(2);
      
      args.forEach(arg => {
        console.log(process.env[arg]);
      });
      

      The forEach method is a standard JavaScript method on all array objects. It accepts a callback function that is used as it iterates over every element of the array. You use forEach on the args array, providing it a callback function that prints the current argument's value in the environment.

      Save and exit the file. Now re-run the program with two arguments:

      You would see the following output:

      Output

      /home/sammy /home/sammy/first-program

      The forEach function ensures that every command line argument in the args array is printed.

      Now you have a way to retrieve the variables the user asks for, but we still need to handle the case where the user enters bad data.

      Step 8 — Handling Undefined Input

      To see what happens if you give the program an argument that is not a valid environment variable, run the following:

      • node echo.js HOME PWD NOT_DEFINED

      The output will look similar to the following:

      Output

      /home/sammy /home/sammy/first-program undefined

      The first two lines print as expected, and the last line only has undefined. In JavaScript, an undefined value means that a variable or property has not been assigned a value. Because NOT_DEFINED is not a valid environment variable, it is shown as undefined.

      It would be more helpful to a user to see an error message if their command line argument was not found in the environment.

      Open echo.js for editing:

      Edit echo.js so that it has the following code:

      echo.js

      const args = process.argv.slice(2);
      
      args.forEach(arg => {
        let envVar = process.env[arg];
        if (envVar === undefined) {
          console.error(`Could not find "${arg}" in environment`);
        } else {
          console.log(envVar);
        }
      });
      

      Here, you have modified the callback function provided to forEach to do the following things:

      1. Get the command line argument's value in the environment and store it in a variable envVar.
      2. Check if the value of envVar is undefined.
      3. If the envVar is undefined, then we print a helpful message indicating that it could not be found.
      4. If an environment variable was found, we print its value.

      Note: The console.error function prints a message to the screen via the stderr stream, whereas console.log prints to the screen via the stdout stream. When you run this program via the command line, you won't notice the difference between the stdout and stderr streams, but it is good practice to print errors via the stderr stream so that they can be easier identified and processed by other programs, which can tell the difference.

      Now run the following command once more:

      • node echo.js HOME PWD NOT_DEFINED

      This time the output will be:

      Output

      /home/sammy /home/sammy/first-program Could not find "NOT_DEFINED" in environment

      Now when you provide a command line argument that's not an environment variable, you get a clear error message stating so.

      Conclusion

      Your first program displayed "Hello World" to the screen, and now you have written a Node.js command line utility that reads user arguments to display environment variables.

      If you want to take this further, you can change the behavior of this program even more. For example, you may want to validate the command line arguments before you print. If an argument is undefined, you can return an error, and the user will only get output if all arguments are valid environment variables.



      Source link

      How To Write Packages in Go


      A package is made up of Go files that live in the same directory and have the same package statement at the beginning. You can include additional functionality from packages to make your programs more sophisticated. Some packages are available through the Go Standard Library and are therefore installed with your Go installation. Others can be installed with Go’s go get command. You can also build your own Go packages by creating Go files in the same directory across which you want to share code by using the necessary package statement.

      This tutorial will guide you through writing Go packages for use within other programming files.

      Prerequisites

      Writing and Importing Packages

      Writing a package is just like writing any other Go file. Packages can contain definitions of functions, types, and variables that can then be used in other Go programs.

      Before we create a new package, we need to be in our Go workspace. This is typically under our gopath. For the example, in this tutorial we will call the package greet. To do this, we’ve created a directory called greet in our gopath under our project space. If our organization were gopherguides, and we wanted to create the greet package under the organization while using Github as our code repository, then our directory would look like this:

      └── $GOPATH
          └── src
              └── github.com
                  └── gopherguides
      

      The greet directory is within the gopherguides directory:

      └── $GOPATH
          └── src
              └── github.com
                  └── gopherguides
                      └── greet
      

      Finally, we can add the first file in our directory. It is considered common practice that the primary or entry point file in a package is named after the name of the directory. In this case, we would create a file called greet.go inside the greet directory:

      └── $GOPATH
          └── src
              └── github.com
                  └── gopherguides
                      └── greet
                          └── greet.go
      

      With the file created, we can begin to write our code that we want to reuse or share across projects. In this case, we will create a function called Hello that prints out Hello World.

      Open your greet.go file in your text editor and add the following code:

      greet.go

      package greet
      
      import "fmt"
      
      func Hello() {
          fmt.Println("Hello, World!")
      }
      

      Let’s break this first file down. The first line of each file needs the name of the package that you are working in. Since you’re in the greet package, you use the package keyword followed by the name of the package:

      package greet
      

      This will tell the compiler to treat everything in the file as being part of the greet package.

      Next you declare any other packages you need to use with the import statement. You’re only using one in this file—the fmt package:

      import "fmt"
      

      Lastly, you create the function Hello. It will use the fmt package to print out Hello, World!:

      func Hello() {
          fmt.Println("Hello, World!")
      }
      

      Now that you’ve written the greet package, you can use it in any other package you create. Let’s create a new package in which you’ll use your greet package.

      You’re going to create a package called example, which means you need a directory called example. Create this package in your gopherguides organization, so the directory structure looks like so:

      └── $GOPATH
          └── src
              └── github.com
                  └── gopherguides
                          └── example
      

      Now that you have your directory for your new package, you can create the entry point file. Because this is going to be an executable program, it is considered best practice to name the entry point file main.go:

      └── $GOPATH
          └── src
              └── github.com
                  └── gopherguides
                      └── example
                          └── main.go
      

      In your text editor, open main.go and add the following code to call the greet package:

      main.go

      package main
      
      import "github.com/gopherguides/greet"
      
      func main() {
          greet.Hello()
      }
      

      Because you’re importing a package, you need to call the function by referencing the package name in dot notation. Dot notation is the practice of putting a period . between the name of the package you are using and the resource within that package that you want to use. For instance, in your greet package, you have the Hello function as a resource. If you want to call that resource, you use the dot notation of greet.Hello().

      Now, you can open your terminal and run the program on the command line:

      When you do, you’ll receive the following output:

      Output

      Hello, World!

      To see how you can use variables in a package, let’s add a variable definition in your greet.go file:

      greet.go

      package greet
      
      import "fmt"
      
      var Shark = "Sammy"
      
      func Hello() {
          fmt.Println("Hello, World!")
      }
      

      Next, open your main.go file and add the following highlighted line to call the variable from greet.go in a fmt.Println() function:

      main.go

      package main
      
      import (
          "fmt"
      
          "github.com/gopherguides/greet"
      )
      
      func main() {
          greet.Hello()
      
          fmt.Println(greet.Shark)
      }
      

      Once you run the program again:

      You’ll receive the following output:

      Output

      Hello, World! Sammy

      Finally, let’s also define a type in the greet.go file. You’ll create the type Octopus with name and color fields, and a function that will print out the fields when called:

      greet.go

      package greet
      
      import "fmt"
      
      var Shark = "Sammy"
      
      type Octopus struct {
          Name  string
          Color string
      }
      
      func (o Octopus) String() string {
          return fmt.Sprintf("The octopus's name is %q and is the color %s.", o.Name, o.Color)
      }
      
      func Hello() {
          fmt.Println("Hello, World!")
      }
      

      Open main.go to create an instance of that type at the end of the file:

      main.go

      package main
      
      import (
          "fmt"
      
          "github.com/gopherguides/greet"
      )
      
      func main() {
          greet.Hello()
      
          fmt.Println(greet.Shark)
      
          oct := greet.Octopus{
              Name:  "Jesse",
              Color: "orange",
          }
      
          fmt.Println(oct.String())
      }
      

      Once you've created an instance of Octopus type with oct := greet.Octopus, you can access the functions and fields of the type within the main.go file’s namespace. This lets you write oct.String() on the last line without invoking greet. You could also, for example, call one of the types fields such as oct.Color without referencing the name of the greet package.

      The String method on the Octopus type uses the fmt.Sprintf function to create a sentence, and returns the result, a string, to the caller (in this case, your main program).

      When you run the program, you’ll receive the following output:

      Output

      Hello, World! Sammy The octopus's name is "Jesse" and is the color orange.

      By creating the String method on Octopus, you now have a reusable way to print out information about your custom type. If you want to change the behavior of this method in the future, you only have to edit this one method.

      Exported Code

      You may have noticed that all of the declarations in the greet.go file you called were capitalized. Go does not have the concept of public, private, or protected modifiers like other languages do. External visibility is controlled by capitalization. Types, variables, functions, and so on, that start with a capital letter are available, publicly, outside the current package. A symbol that is visible outside its package is considered to be exported.

      If you add a new method to Octopus called reset, you can call it from within the greet package, but not from your main.go file, which is outside the greet package:

      greet.go

      package greet
      
      import "fmt"
      
      var Shark = "Sammy"
      
      type Octopus struct {
          Name  string
          Color string
      }
      
      func (o Octopus) String() string {
          return fmt.Sprintf("The octopus's name is %q and is the color %s.", o.Name, o.Color)
      }
      
      func (o Octopus) reset() {
          o.Name = ""
          o.Color = ""
      }
      
      func Hello() {
          fmt.Println("Hello, World!")
      }
      
      

      If you try to call reset from the main.go file:

      main.go

      package main
      
      import (
          "fmt"
      
          "github.com/gopherguides/greet"
      )
      
      func main() {
          greet.Hello()
      
          fmt.Println(greet.Shark)
      
          oct := greet.Octopus{
              Name:  "Jesse",
              Color: "orange",
          }
      
          fmt.Println(oct.String())
      
          oct.reset()
      }
      

      You'll receive the following compilation error:

      Output

      oct.reset undefined (cannot refer to unexported field or method greet.Octopus.reset)

      To export the reset functionality from Octopus, capitalize the R in reset:

      greet.go

      package greet
      
      import "fmt"
      
      var Shark = "Sammy"
      
      type Octopus struct {
          Name  string
          Color string
      }
      
      func (o Octopus) String() string {
          return fmt.Sprintf("The octopus's name is %q and is the color %s.", o.Name, o.Color)
      }
      
      func (o Octopus) Reset() {
          o.Name = ""
          o.Color = ""
      }
      
      func Hello() {
          fmt.Println("Hello, World!")
      }
      

      As a result you can call Reset from your other package without getting an error:

      main.go

      package main
      
      import (
          "fmt"
      
          "github.com/gopherguides/greet"
      )
      
      func main() {
          greet.Hello()
      
          fmt.Println(greet.Shark)
      
          oct := greet.Octopus{
              Name:  "Jesse",
              Color: "orange",
          }
      
          fmt.Println(oct.String())
      
          oct.Reset()
      
          fmt.Println(oct.String())
      }
      

      Now if you run the program:

      You will receive the following output:

      Output

      Hello, World! Sammy The octopus's name is "Jesse" and is the color orange The octopus's name is "" and is the color .

      By calling Reset, you cleared out all the information in the Name and Color fields. When you call the String method, it will print nothing where Name and Color normally appear because the fields are now empty.

      Conclusion

      Writing a Go package is the same as writing any other Go file, but placing it in another directory allows you to isolate the code to be reused elsewhere. This tutorial covered how to write definitions within a package, demonstrated how to make use of those definitions within another Go programming file, and explained the options for where to keep the package in order to access it.



      Source link

      How to Write Meta Descriptions That Get Clicks (5 Key Tips)


      Search engines can make or break websites. Getting your site to show up on Search Engine Results Pages (SERPs) often isn’t enough. You also have to get people’s attention, so they’ll click on your links over the hundreds of other options.

      At their core, meta descriptions give potential visitors an overview of what kind of content they can expect. They tend to be just a few lines long, so small differences in the way you write your meta descriptions can be enough to boost your click-through rate significantly.

      In this article, we’re going to talk about what meta descriptions are, why they’re necessary, and what elements they should include. Then we’ll walk you through five tips to ensure that your meta descriptions hit home every time. Let’s get to it!

      Shared Hosting That Powers Your Purpose

      We make sure your website is fast, secure and always up so your visitors trust you. Plans start at $2.59/mo.

      An Introduction to Meta Descriptions

      Meta descriptions are the snippets of text you see underneath the title within SERPs, as in the example below.

      Two examples of meta descriptions.

      The main goal of a good meta description is to give you an idea of what the page is all about. Naturally, titles also play a vital role here, but there’s only so much information you can fit into a single headline.

      Meta descriptions provide you with up to a couple of sentences to expand on your page’s content. You can either write them yourself or have search engines generate them automatically based on each user’s search query.

      As convenient as having search engines do the work for you sounds, however, we strongly recommend that you write your own meta descriptions. That way, you get full control over what shows up on the SERPs and on social media sites while also increasing your chances of engaging users.

      Let’s take a look at some meta description examples for a specific line of shoes. You can tell the meta description below was generated automatically, and it doesn’t give you much to go on.

      An example of an unoptimized meta description.

      Here’s another result for the same product search, this one using a stronger meta description.

      An example of an optimized meta description.

      It’s important to understand that meta descriptions only give you a limited number of characters to play with. On desktops, that can be up to 158 characters, whereas mobile users will only see 120 of them. Roughly speaking, that means you get about two lines of text.

      Why Meta Descriptions Are Important

      Search Engine Optimization (SEO) is all about competition. You compete against every other site that appears within the results pages for a given search, each hoping to get the lion’s share of the clicks.

      When it comes to the SERPs, several factors determine how many views your links get, including:

      • The title you use
      • Whether it’s a rich snippet or not
      • If it appears within an answer box
      • The position of your pages
      • Your meta descriptions

      Out of all those factors, you get full control over three of them: your title, schema markup, and meta descriptions. It’s only logical that you should optimize those elements as much as possible.

      If you take another look at the previous section, you’ll notice just how much of a difference a good meta description can make. Letting search engines generate yours will often result in descriptions that look like gibberish.

      What to Include in a Meta Description

      Two lines of text aren’t much, but more often than not, it’s enough to cover a few key elements. Most often, this should include:

      • What your page is about
      • How it can benefit the reader

      If a meta description is too vague, then you’re not selling users on the idea of visiting your website. You’ll still get clicks, of course, but not as many as you might have otherwise.

      Let’s say, for example, that you wanted to write a meta description for this article. Here’s a not-so-good example:

      Have you ever wondered what meta descriptions are? Wonder no more, because we’ll tell you everything you need to know.

      While it hits on the article’s primary topic, it doesn’t do a good job of previewing the page’s actual content. Now let’s give it another go, keeping in mind the fundamental elements we want to include:

      Meta descriptions are key to any site’s SEO. In this article, we’ll break down why and help you optimize your own descriptions. Read on to find out more!

      This is short and to the point, and we even had enough characters left over to include a simple Call to Action (CTA). It may not win any literary awards, but it will get the job done.

      How to Write Meta Descriptions That Get Clicks (5 Key Tips)

      At this point, you know the basics of what a meta description should include. However, if you want your descriptions to really hit home, here are five tips to help you optimize them further.

      1. Use Relevant Keywords

      If you’re reading this, you’re probably familiar with the concept of keywords. Ideally, you’ll use them organically throughout all of your content, and that includes metadata such as your descriptions.

      Let’s say, for example, that you’re writing a recipe and you want to optimize it for the search term “how to cook a healthy lasagna.” That’s an easy to term to work into a meta description:

      Learning how to cook a healthy lasagna is easier than you might imagine. Let’s go over a recipe you can cook in under two hours!

      Including keywords within your meta descriptions is a smart SEO practice. It gives search engines a better idea of what your content is all about. However, as always, make sure to work those meta keywords in organically. That means not stuffing your descriptions full of keywords; make your description still reads like something a human (not a bot) would write.

      2. Don’t Obsess Over the Character Count

      So far, most of the examples we’ve shown you have come in well under the maximum character count for the major search engines. You want to get some mileage out of your meta descriptions, but in practice, obsessing over the character count isn’t as serious as you might think.

      To build on our earlier example of a healthy lasagna recipe, you could easily expand on its description to cover more information:

      Learning how to cook a healthy lasagna is easier than you might imagine. For this recipe, we’re substituting meat with eggplants, which means it will cook faster and feed up to four people.

      That example goes over the character limit for both desktop and mobile meta descriptions in Google. In practice, it would get cut off and look something like this:

      Learning how to cook a healthy lasagna is easier than you might imagine. For this recipe, we’re substituting meat with eggplants, which means it will cook …

      That snippet still provides plenty of information, so you don’t necessarily need to change it. What matters is that you include the essential details early on, so whatever does get cut off is just supplementary information.

      3. Optimize for Rich Snippets

      Most search results look pretty dull — a sea of titles, meta descriptions, and URLs. However, in some cases, your results will look a bit more lively.

      Three examples of carbonara recipes with rich snippets.

      Those are examples of rich snippets. To create them, you add structured data markup to your pages, providing more information on what their content includes. Search engines can recognize that information and structure your results accordingly.

      This practice offers two key benefits:

      1. Your pages will look more engaging within the SERPs.
      2. You get to add a ton of extra information to your results, without needing to count characters.

      For a real example, let’s take a look at the results for “how to cook a healthy lasagna.”

      Two healthy lasagna recipes with rich snippets.

      Two of the top results are featured snippets. Without even clicking on them, you can see an image, cooking time, rating, and even the number of calories in the recipe.

      Keep in mind that not all types of content lend themselves well to rich snippets. However, they’re pretty easy to implement, once you know how to add the right structured data markup to your pages.

      4. Avoid Duplicates

      When it comes to meta descriptions, there are two kinds of potential duplicates. It’s good practice to avoid both of them:

      1. Mimicking other sites’ descriptions
      2. Having several of your pages use the same description

      Overall, duplicate content is almost always bad news when it comes to SEO. Moreover, it can hurt your click-through rate if you have several pages competing for the same search terms.

      For practical purposes, there’s no reason all of your pages shouldn’t have unique meta descriptions. If it takes you more than a couple of minutes to write one, then you’re probably overthinking it.

      5. Use Interesting Words

      Most meta descriptions are pretty boring, at least linguistically speaking. The need to cover so much information in such a limited space doesn’t lend itself well to innovation.

      One way to make your meta descriptions stand out is by using compelling language. To do that, take a look at what other websites are writing for the keywords you want to rank for. Let’s say, for example, that you’re looking for a cast iron pizza recipe.

      A lot of the content will be similar, which means their meta descriptions will share elements as well. However, not all descriptions are equally effective.

      Some examples of cast iron pizza recipes.

      Some of our favorite hits from the above example include the words ‘crispy,’ ‘buttery,’ and ‘chewy.’ There are five results here, but the first and last stand out due to their word choices.

      Think about it this way — if you’re staring at that page trying to decide which recipe to follow, you’ll probably pick the one that sounds more delicious. At that stage, you don’t know how good the recipe will be, so your only indicators are the title tag, picture, and word choice in the meta description.

      Search Result Focus

      When you boil it down, SEO is a competition. You’ll never be the only website within a niche, so you need to look for ways to make your pages stand out in the SERPs. Fortunately, an informative, unique meta description is a great way to catch potential visitors’ eyes.

      Are you looking for a hosting plan that can handle all the traffic your improved meta descriptions will send your way? Check out our shared hosting options!



      Source link