Basic Javascript Syntax

Página 2

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:


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.

Next page of the Free Ebook:

3Variables and Data Types in Javascript

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text