Introduction to Programming
What is Programming?
Computers require precise instructions in order to perform actions or complete tasks. Humans write programs or software that can then be turned into instructions that computers can understand in order to perform tasks. Programming languages provide a set of rules for writing out instructions that can be used by humans but that will ultimately broken down into precise instructions for a machine
Computer programs
A computer program is a set of instructions written for machines to execute. Machine themselves only deal with binary data at a fundamental level. Binary data means bits (short for binary digits which are either 1s or 0s.) This data is used to carry out tasks by the computer. We could in theory write code which is just a series of bits however it is incredibly difficult for humans to reason about code at this level. A higher level programming language provides greater abstraction away from machine language so that humans can write instructions that will ultimately be executed by a computer. Higher level languages are designed to make writing instructions far simpler: eventually these programs will be converted into machine language so the machine can execute them.
JavaScript
JavaScript is a high level programming language and one of the core technologies used in the the world wide web. Since then over the course of over 2 decades it has rapidly become one of the most popular programming languages in the world.
Data and functions
A fundamental concept in computer programming and within JavaScript is the notion of data and functions. Data could be any piece of information from numbers to strings of text. A programming language will usually have different fundamental categories of data known as primitive data types. Primitive data types are the most basic data types we can use in a programming language. One of these primitive data types in JavaScript is a string. A string is a piece of text enclosed inside quotation marks, written like this:
'hello devups';
A function is a set of instructions within a program that can be used to perform an action. A large part of programming involves writing functions that will carry out meaningful tasks with our data types. Functions are one of the fundamental units of JavaScript. A function will often be designed to take inputs, carry out some tasks and provide an output.
Think of a function called add. This function will take in number inputs (some data) and then produce an output. So if we passed in the input values of 10 and 32 into our add function we could expect it to output a value of 42. It is our task as developers to implement or build functions that will carry out tasks like the one previously mentioned.
Our first program
"hello devups"
We can now think about writing our first JavaScript program using some data and a function. We are going to use a file called example.js and in it write the following piece of code. In order to save time and make it easy for developers to write software, programming languages will often come with pre-made built-in functions or tools in order to carry out certain kinds of important work.
console.log is one such tool or function. The piece of code introduces the function console.log - an very and commonly used function in JavaScript.
Once we have written the function we can update our code in order to actually use the function. Unless we actually write the code to say we are using the function then nothing will happen.
In order to use console.log we must then call the function which you can think of as meaning "use the function", like below:
console.log('hello devups!');calling/invoking a function means running the function so its instructions are executed.
The example above consists of 3 different parts:
-
console.log;
- the name of a function, in this case
console.log
-
console.log();
- parentheses, this notation after the name of a functions means we are calling the function
-
console.log('hello devups');
- an input, which is the string of text:
"hello devups"
We can now use our run-time environment NodeJS (opens in a new tab) in order to run this piece of code. We can use the following command:
$ node example.jsOnce example.js is executed we can see that something has been printed to our console.
console.logis an in-built function we can use in order to print information or data to our console
Strings
The input into our function console.log was the following piece of data: "hello devups"
Any characters enclosed inside quotation marks " or ' is known as a string.
We can think of this as type of data used for storing textual information.
'hello devups';
Comments
Comments are parts of a program that will be ignored by the computer. Comments in JavaScript are denoted by // starting on a line like in the example below:
console.log('hello devups');
// this will print the message 'hello devups' to the screenWe can also create a comment by enclosing text inside /* and */. For example,
console.log('hello devups');
/*
this will print the message 'hello devups' to the screen
another comment on here too!
*/