Javascript For Beginners. Lesson 2 - Common programing concepts.
In this lesson I am going to explain basic common concepts of programming. So I will try to answer questions: What compiler/interpreter is? What source code is? Basic elements of your program. etc
Speak on the same language with your computer
As you probably know we could not speak with our computer using human language (excluding cases when we use special software). Sure we cans speak but it will look silly and unproductive. That is why we need something to translate human language to language of zeros and ones - Binary code.
We can split programming languages in to two big groups Scripting languages and Compiled languages. In several words:
- Scripting language has only Interpreter which directly translate commands from source code into Machine code which is native language of CPU of your Computer.
- Compiled languages has Compiler which convert you source code into bytecode and p-code (It is something in the middle of source code and Machine code) and Interpreter which translate bytecode into [Machine code] simply speaking into your program itself. Both groups has advantages and disadvantages but we will not speak about it in this lesson.
What source code is
In several words Source Code is collection of commands or even instructions written in human-readable programming language, which usually looks like a plain text. I case of JavaScript we will call it a script.
Here is an example of JavaScript code which just print greeting message.
See the Pen tiny js by Alexander Bezverkhniy (@alex-bezverkhniy) on CodePen.
In my further lessons I will explain meaning of document
and write(...)
, so far you need to remember that we use it for output something on screen.
Syntax and Basics of JavaScript
Syntax of JavaScript and any other programming languages is the set of rules that define a correctly structured program. In other words it is like grammar for human language. We have to fallow these rule to make our program (or source code) readable for interpreter. We could not say or write to interpreter “let’s write ‘Hi’ on the screen” it does not understand us.
Lets start from the practical example:
See the Pen eyqQMB by Alexander Bezverkhniy (@alex-bezverkhniy) on CodePen.
Tab JS - JavaScript code. Tab Result - output of this script. This script is your first “Hello World program” ;-) You can edit this code by clicking on “edit on codepen”. in right tom corner.
Here is code explanation (by lines):
- Creates variable with name
myVariable
and set value'Hello World!'
- Uses method
write
of objectdocument
for writing value of variable onto document.
If you click on Result tab you could see result of this program.
Now let’s dive into theoretical part of JS.
Reserved Words or Keywords
All programming languages have Keywords it is Reserved Words for special language purpose. We ca consider these words as special command. You can find full list of keywords here.
For example keyword var
, from our previous example, defines variable.
Variables
Important part of all programming languages is Variables. In several words, it is storage location in memory with specific name. In general Variables works like a big catalog-cabinet where each box has name and you can easily put and get values using that name.
Photo by Erol Ahmed on Unsplash
Here is examples how to declare variable:
1
var myFirstVariable;
To set or give a value to variable we use special operator =
.
1
var myVariable = 'Hello World!';
A semicolon at the end of a line indicates where a statement ends; it is only absolutely required when you need to separate statements on a single line.
JavaScript is case sensitive — myVariable
is a different variable to myvariable
. If you are getting problems in your code, check the casing!
You can use any name for your variable by here is same restrictions. You can check variable name here.
Now time for practice!
Clink on to