#3 Learning the basics of JavaScript: Queues and stacks, libraries, arithmetic and strings

I’ve already published two previous posts on the basics of learning JavaScript. This is the third installment. If you missed the previous posts, you’re welcome to catch up on them first.

#1 Learning the basics of JavaScript: variables, arrays, conditional statements and functions

#2 Learning the basics of JavaScript: the DOM, syntax, nodes

What can I do with JavaScript?

Remember that when you learn JavaScript, you’re learning in the context of your own education. The reality is that nowadays, professional developers mostly work in teams. Modern software is too complex for one person to develop on their own.

If you want to focus on building on your own project or app for fun, that’s fine, too. But you are going to be learning slightly different skills than if you want to become a professional software developer.

I would like to be able to program so I can become a documentation writer for software, so I’m focusing on becoming proficient in JavaScript to get started. This is because I’m a professional writer, so realistically becoming a developer is not on my career path.

I would also like to become a contributor to open source projects because this is something I value. Some of them can be for a really great cause, or just be super handy, like GIMP, the Free and Open Source (FOSS) photo editing software.

What the heck is open source?

Don’t know what open source is? It goes by multiple names depending on your philosophical leanings, but essentially it means that a program’s software code is open to modification by its users and can be freely distributed.

Image: Linus Torvalds, the creator of the open-source Linux Kernel and software version control system Git, Wikimedia Commons

Most software available now is proprietary, and that means particular companies own the code. They want to use this model of owning software to make a profit, unlike in the early days of the web, when nerdy men used to bootleg software and share it freely.

Now, let’s learn some more JavaScript.

How software languages came to be

If you think about the concept of computer science and science and engineering in general, these refer to the aspects of the external world that humans have built as part of a collective effort. Science and engineering have enabled us to build atomic bombs, 3D printers and stethoscopes.

In contrast to spoken languages, which organically developed over time from the first grunts of cave men to the erudite heights (or not so much) that we know today, programming languages were actually made up by specific people. It took me ages to get my head around this.

The founder of JavaScript was Brendan Eich, who also co-founded Mozilla (the foundation behind the open source browser Firefox), and wrote it for a specific purpose (to build the original Firefox browser, Netscape Navigator).

JavaScript, like all programming languages, has been created for a specific technological purpose and is documented online. Since JavaScript is so popular, there are lots and lots of resources out there devoted to teaching people the language. DigitalOcean has some great resources for beginners written in refreshingly plain language.

Javascript libraries

JavaScript libraries are built with the same principle in mind. They are simply collections of scripts that contain functions written by others for a specific purpose, and which are deemed to have lasting usefulness. This means that you don’t have to write as much new JavaScript code on your own, and instead, you can benefit from the hard work of others.

If you want to use a JavaScript library such as Galleria, jQuery, or MooTools, you have to install it in your programming environment. When learning to program, you need to get to grips with the command line because this is the way that you will be interacting with the server from your computer. The server is another computer that performs certain functions for the client (in this case, your computer) and hosts the database.

When you’re writing a new JavaScript program, you might want to affect something on the web page for your user. A common way to learn JavaScript is to play around with pop-ups, which you can cause to open based on a user action. It’s also a handy way of testing if your code works.

Image: Library at Trinity College Dublin, Wikimedia Commons

Queues and stacks

When you write JavaScript, useful units to work with are queues and stacks. In programming, these are used to structure data, because, to be honest, programming is all about manipulating data.

When you program anything, it’s generally about handling data in some way (information that a computer understands) to achieve a stated purpose.

In a queue, you store data in a linear structure (a line that has a beginning and an end), and the data that was added to the queue first gets deleted.

In a web browser, if it’s being a little slow, you notice that the action you took first (such as clicking on a button) is executed before all the other actions you performed subsequently. This is a real-life use of a queue. You can remember this type of queue as First In First Out (FIFO).

Image: Queue of people, Pixabay

A stack is just the opposite. Whatever was added most recently to a stack is deleted first, and can be remembered as Last in First Out (LIFO). You might use a JavaScript stack for the browser’s back button. Michael Mitrakos has written a good post about stacks on Medium.

It’s not so much about writing specific code to create a queue or a stack, as it is about how you remove data from the array (list). If you can’t remember what an array is, check out my first JavaScript post.

Queue operations:

enqueue() adds data
dequeue removes the oldest added data

Stack operations:

push() adds data
pop() removes the most recently added data.

Strings

In JavaScript, strings are made up of letters, numbers and symbols. In programming, it’s how we represent and work with text-based data and they can be applied across many languages.

You know something is a string if it is surrounded by quotes. The type of quotes you use isn’t that important, unless you’re writing a Template Literal.

For example, “Hello, World!” is a string and it can be stored inside variables to be used later. Users can also type their own strings into the browser.

Strings can be added together (called concatenating), so a typical use-case would be something like “Hello” + “Catherine” to address a user with personalization.

To learn more about strings, visit DigitalOcean’s great article about JavaScript strings.

Arithmetic

Obviously, as programming deals with lots of numbers, it’s possible to perform arithmetic and JavaScript is no exception.

You can do all the obvious things like add and subtract, multiply and divide, as well as ‘modulo’ find the remainder (used in some common functions in science, like converting celsius to Fahrenheit).

A real-life use case of JavaScript in the browser might be to calculate the final price of a transaction, or how many posts to load in a blog roll.

In many of your functions, you will be performing equations with your data. Numbers can also be stored inside variables (like strings) to use later.

Unsurprisingly, DigitalOcean has another fantastic JavaScript arithmetic guide for beginners.

Final remarks

Many people want to learn to code to advance their current job, or to transition into a role in the technology industry.

It’s also important to simply understand the technology that underpins our lives. Learning to code, and particularly about computer science, is a great way to do just that.

Girls and women are discouraged from making and breaking things. In films, literature and the media it’s seen as the domain of men, and that’s a shame.

I recently bought a Raspberry Pi, a mini computer specifically marketed to beginners (actually children!) learning to program. It uses the languages Scratch and Python, but you can try it with JavaScript as well. It costs around £30 but I think it’s a great way to enter the world of programming.

Did you like this post? Contact me at catherine@awaywithwords.co to commission me to write for your company blog.

About the author

Catherine Heath

Catherine is a freelance writer based in Manchester. Blogs. Copy. Documentation. Let's ditch the jargon – just give her plain writing.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *