JavaScript: The Good, the Bad and the Ugly

0
90

JavaScript is one of the most powerful programming languages in existence, yet it suffers from a bad rap. This post explores various topics compiled by Business News Web, hopes to educate the masses about JavaScript’s upsides and downsides, and clear up some misconceptions about this stellar language.

Technology, Computer, Code, Javascript, Developer

What you will learn:

 

– What JavaScript is   

Advertisement

– The pros and cons of using JavaScript in your code

 

Hopefully this helps equip everyone with the knowledge they need to make an educated decision on whether they want to explore JavaScript more deeply.

 

What is JavaScript?

 

JavaScript is a programming language. It’s probably one of the easiest programming languages to learn, as it’s used in today’s web browsers. For example, here’s a little script which I wrote when I last needed to change my Minecraft skin:

 

Pretty simple, right? One neat feature about JavaScript is that you can use it on websites that you visit (like YouTube, for example). You can even write your own programs for JavaScript and publish it online!

 

JavaScript is an easy language to learn – here are some resources if you’re interested in learning more:

 

Why use JavaScript?

 

JavaScript is a very convenient tool for programming stuff. This post is not about making web pages or apps, but rather about getting a better understanding of JavaScript. Learning how to make cool things with JavaScript will come with time. For now, though, you probably just want to learn more about it so you can become a more proficient programmer.

 

How do you use JavaScript?

 

In order to get a feel for what good and bad methods in JavaScript are, follow these steps:

 

1) Download an online version of the “Hello World” script from the Internet

 

2) Open up Firefox and right click in the browser window and choose “view page source” to look at the HTML code. (This will also work in IE – I’m just using Firefox as an example).

 

3) Once you’re in the HTML source, look for the “Hello World!” line:

 

4) When you’ve found it, copy/paste that line into Notepad. You should then have something similar to this:

 

5) Now that you’ve got your “Hello World!” script open in Notepad, look for other “Hello World” scripts on the web and copy/paste them into your Notepad file. Keep doing this until you have at least 10 or so “Hello World!” scripts in the document.

 

6) Now that you have a big wad of code with different ways JavaScript can be written, it’s time to analyze the differences between them. 

 

JavaScript – The Good

 

The first thing I want to talk about is hoisting in JavaScript. Hoisting is a way of saving space in script code by moving some of the code to where it’s actually going to be used, rather than keeping it organized in a “global” variable and assigning it within your function/function calls.

 

Take this example:

 

If you were to hover over the variable “name”, you would see that it is a global variable. This means that it can be accessed anywhere in the script. Because of hoisting, “name” will be assigned to whatever is inside the quotes where it is used. In this example, it’s assigned to what’s between the quotes when a function is called. This means that inside the function call code, we can still access name:

 

Now let’s take a look at situations where we don’t have hoisting:

 

I’ve added a new line in here which assigns another variable called “yay” to whatever number ends up being returned in the line above. Because of hoisting, we can’t do this:

 

If hoisting was not in use, then the script would look more like this:

 

JavaScript – The Bad

 

This is where it gets fun. There are some things to be aware of in JavaScript if you want to write good code that’s efficient:

 

To properly use these tips, you’ll need to know about scope and closures. Scope refers to when and where a variable is created and what it’s used for. 

 

Closures are essentially the way that you can access variables that are outside of a function, without having to use global variables.

 

You should now have a good understanding of how scope and closures work in JavaScript. When you use them properly, your code will be easier to read and follow.

 

JavaScript – The Ugly

 

There’s not that much ugly in JavaScript, but there are some things to be aware of. Some people say that JavaScript is the best language ever, while others call it evil. What’s the truth? Let’s find out!

 

First of all, global variables should be avoided as much as possible. If they exist at all, then they should be very small. For example:

 

Global variables should also be avoided if possible. It’s better to have them in functions and use function scope instead. A good example is as follows:

 

Alternatively, you can put global variables into “namespaces”. Namespaces are basically two or more global variables that you group together so that you can keep track of one object instead of multiple objects (one for each namespace). 

 

Meta Description:

 

JavaScript is a programming language that allows you to write your code in today’s increasingly complex web development.

LEAVE A REPLY

Please enter your comment!
Please enter your name here