(click anywhere to close)
OPEN MENU

[JavaScript] Basic Syntax

category: Website | course: JavaScript | difficulty:
IN PRINT
QUICK CONTENTS:Intro

A programming language works by executing statements in a certain order. Therefore, generally speaking, everything you write is either a statement, or determines which statements to execute and when.

JavaScript always reads statements in the same code block from top to bottom. To signal the end of one statement, you must always append a semicolon ( ; ). The parts that are not statements do not need to end with a semicolon, but have their own way of opening and closing itself – usually braces ( { } ).

somestatement;
anotherstatement;

notSomeStatement {
	/* Code block here... */
}

Statements can either declare a variable, or assign a value to a variable (or both at the same time). Once we look at variables, you’ll see that you can assign fixed values as well as other variables. That’s great and all, but that doesn’t add any practical value, because you’re just calculating and saving things – not even displaying anything on the screen.

Therefore, statements in JavaScript can also change elements of a webpage or call functions. This way you can, for example, change the background colour, or request information from a server when the user clicks a button.

//Example: changing the text colour of the element with id someId to white
document.getElementById("someId").style.color = 'white';

The other parts determine, mostly by using logical operators to compare values, when to use certain parts of the code. For example, you could create a button that changes the background colour, but only if today is a Sunday. And lastly, there’s loops that determine how often a certain block of statements is executed.

//Example: if today is monday, execute the console.log statement 10 times.
var today = new Date().getDay();

if(today === 1) {
	for(var i=0;i<10;i++) {
		console.log('Yay! Today is monday!');
	}
}

These concepts may all seem a bit vague and general now, but I just wanted to give a global overview of what’s at the core of this scripting language. The rest of the course will explain all of these things in great detail.

CONTINUE WITH THIS COURSE
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)