DEV Community

Cover image for Thread of Execution - One Thing at a time
Leonardo Cunha
Leonardo Cunha

Posted on • Updated on

Thread of Execution - One Thing at a time

OK, you just started learning a programming language and fell in love with JavaScript. However, do you still need help with the code? Guessing different scenarios until you achieve your goal?

Please don't worry; this is normal. I just want you to know that you are not alone in this endeavour.

Although, I want to help you. So you can get more control of your code. You can understand more about what is happening "under the hood".

One thing at the time

The first thing you need to understand to gain more control of your code is to know more about the role of the Thread of Execution(TE).

By design, JavaScript was created to execute one thing at a time(Single Thread) and in a specific order(top -> bottom). It's responsible for executing line by line of your JS code.

"I see, but what do you mean by executing?" you might ask. Let's go through an example.

Example

Suppose we wrote the following JavaScript code:

const author = 'Leo'

function validateAuthor(author) {
if(author === 'Spock') {
return true
}
return false
}

validateAuthor(author)

Enter fullscreen mode Exit fullscreen mode

Alright, let's dive in:

1- Where have all the constants gone?


const author = 'Leo'
Enter fullscreen mode Exit fullscreen mode

Here, we declare a constant named author and assign the value 'Leo'.

And not only that. We also store this constant in our memory. We can access the value 'Leo' using the variable author.

2- "Sit tight and assess..."


function validateAuthor(author) {
if(author === 'Spock') {
return true
}
return false
}

Enter fullscreen mode Exit fullscreen mode

Functions are a set of statements that performs a task or calculates a value.

Although, we still need to execute this function.
And for that, we need the "()".

For now, the TE stores this whole block in our memory. And like we see above, allowing us to use it later.

3- "Call me"


validateAuthor(author)

Enter fullscreen mode Exit fullscreen mode

At last, we are just calling/executing our function. The TE checks the label we use, looks into the memory and runs it.

And not only that. Please take a look at the label author inside the parenthesis. This means that we wanna use the value of the label author('Leo'), which is already stored in our memory, as a parameter in our function.

Therefore, we grab the value 'Leo' and check if the string equals 'Spock'.

That's it.

Practice!

Of course, this is a simple example. However, it would be best for you to start to create a level of understanding of how your code will be executed. Thus avoiding the guessing habit.

And the only way to improve that, it's with practice. The Rubber duck debugging method might be helpful.

After that, you can search for what I mean by "memory" and so on. Curiosity is an excellent tool for us developers.

Until the next JS topic!

Latest comments (0)