Free Ebook cover How to use Javascript in website development

How to use Javascript in website development

5

(2)

37 pages

Basic Javascript Syntax

Capítulo 2

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Javascript is a programming language that is used to add interactivity and dynamism to web sites. With Javascript, you can create visual effects, validate forms, perform calculations, among other features.

The basic syntax of Javascript is composed of several structures, such as variables, conditionals, loops, functions, among others. Some of these structures will be presented below:

Variables

Variables are used to store values ​​that can be used later in code. To declare a variable in Javascript, the reserved word "var" is used, followed by the name of the variable and the value that will be assigned to it. For example:


var name = "John";
var age = 25;

In this example, two variables were declared: "name", which receives the value "John", and "age", which receives the value 25.

Conditionals

Conditional structures allow code to perform certain actions only if a certain condition is met. In Javascript, the most common conditional structures are "if" and "else". For example:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app


var age = 18;

if(age >= 18){
    alert("You are of legal age!");
} else {
    alert("You are underage!");
}

In this example, it is checked if the variable "age" is greater than or equal to 18. If the condition is true, an alert is displayed stating that the person is of legal age. Otherwise, an alert appears stating that the person is a minor.

Loops

Loops are used to perform certain actions repeatedly. In Javascript, there are two types of loops: "for" and "while". For example:


for(var i = 0; i < 10; i++){
    console.log(i);
}

var j = 0;
while(j < 10){
    console.log(j);
    j++;
}

In this example, the "for" loop is used to display the numbers 0 through 9 to the console. The "while" loop is used to display the same numbers, but in a different form.

Functions

Functions are used to group a set of instructions that can be executed in different parts of the code. In Javascript, functions are declared using the "function" keyword. For example:


function sum(a, b){
    return a + b;
}

var result = sum(2, 3);
console.log(result);

In this example, a function called "sum" was declared, which takes two parameters (a and b) and returns the sum of these values. Then the function is called passing the values ​​2 and 3 as parameters, and the result is displayed in the console.

These are just some of the basic structures of Javascript. With these structures and more advanced ones, it is possible to create dynamic and interactive websites.

Now answer the exercise about the content:

_Which of the structures below is used to execute certain actions repeatedly in Javascript?

You are right! Congratulations, now go to the next page

You missed! Try again.

The structure used to execute certain actions repeatedly in Javascript is called loops. Loops include structures like 'for' and 'while', which allow you to perform actions multiple times based on a specified condition.

Next chapter

Variables and Data Types in Javascript

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.