Blogpost

Published: 2020-06-23

Learn programming, it's easy

How to get started in becoming a programmer. Let's write your first code.

Many programming languages require a lot of setup in order to see a first result. When you are new to programming you want to see a result after writing your first line. So let's right jump into it. Write your first line of code. I recommend using the Chrome browser, but this should work in other browsers as well.

Messy javascript code

Table of contents

First line of code

You are already in the browser, so hit F12 (fn+F12 on mac) and the console will open up, don't be afraid, instead click on the "Console" tab. It's highlighted on the image below.

Chrome console

Write console.log('hi') as on the image, then hit enter. The console will say "hi" to you, exciting, hm? Congratulations, you have executed your first line of code. You are now a coder. Somehow 😁.

What is programming?

Programming is telling the computer what to do, it's similar to shout at your kids or your dog, but the computer will actually care. You provide some instructions and in the simplest case it will execute one after the other with no side effects. In more complex scenarios a lot of code will run simultaneously and a lot of communication between the different "running lines" will occur.

Multiple instructions - your first function

Let’s glue some instructions together to form a program. You can click on the text in order to copy it to your clipboard. Paste it into the console or type it down, then again, hit enter. 💣

function printName (name) {
    console.log(name);
}

let myName = 'sepp';
printName(myName);

Now what happened here?

function printName (name) {
    console.log(name);
}

In this snipped we created a function. A function is a sequence of code that can be called from another point in the program by passing zero or more parameters into it ("name" in the example is a single parameter). In the function - between "{" and "}", that wrap the body of the function - we can then use the handed in parameters/variables.

let myName = 'sepp';

Declares a variable named "myName" with the value "sepp". This allows us to reuse that value using "myName", the variables name.

printName(myName);

Lastly we call the previously created function "printName", and pass in our variable "myName". This will result in the execution of our function body, using the handed in value of the variable "myName", that is "sepp", as variable "name". The body of the function consists of a solely statement console.log(name); that we know from the previous chapter. This time it will print "sepp" to the console. In difference to the previous usage of this statement we do not pass the value directly but the variable instead (we can always swap a value with a variable and vice versa, but we have to declare the variable first, as we did with let myName = 'sepp';). This was your first program, pin it to your wall, you are now a programmer. 🤓

Sources

Tags

#tutorial #learn_programming #javascript