Javascript Syntax

We started to study the syntax of the Javascript language, stopping at its main features, such as the use of upper and lower case letters, comments, semicolons and many other things.

We finally started to see Javascript source code! We hope that you have assimilated all the previous information in the , in which we have basically learned various ways of including scripts in web pages. Until now, everything we have seen in this manual may have seemed very theoretical, but from now on we hope that you will find it more enjoyable as you begin to see more practical things directly related to programming.

The Javascript language has a syntax very similar to that of Java because it is based on it. It is also very similar to that of the C language, so that if the reader knows either of these two languages, it can be easily handled with the code. Anyway, in the following chapters we are going to describe all the syntax in detail, so newbies won’t have any problem with it.

Comments in the code

A comment is a piece of code that is not interpreted by the browser and whose usefulness lies in making it easier for the programmer to read. As the programmer develops the script, the programmer leaves phrases or single words, called comments, which help him or anyone else to read the script more easily when modifying or debugging it.

Some Javascript comments were already seen before, but now we are going to count them again. There are two types of comments in the language. One of them, the double slash, is used to comment a line of code. The other comment can be used to comment several lines and is indicated with the signs /* to start the comment and */ to end it. Let’s see some examples.

See also  Deleting a record from the database with PHP

Upper case and lower case

In Javascript, upper and lower case must be respected. If we make a mistake when using them, the browser will respond with an error message, either syntax or undefined reference.

For example, the alert() function is not the same as the Alert() function. The first shows a text in a dialog box and the second (with the first capital A) simply does not exist, unless we define it. As you can see, for the function to be recognized by Javascript, it must be written in all lowercase. We will see another clear example when we deal with variables, since the names we give to variables are also case sensitive.

As a general rule, the names of things in Javascript are always written in lowercase, unless a name with more than one word is used, in which case the initials of the words following the first will be written in uppercase. For example document.bgColor (which is a place where the background color of the web page is stored), is written with the “C” of color in capital letter, because it is the first letter of the second word. You can also use capital letters in the initials of the first words in some cases, such as the names of the classes, although we will see later what these cases are and what the classes are.

instruction separation

The different instructions that our scripts contain must be conveniently separated so that the browser does not indicate the corresponding syntax errors. Javascript has two ways to separate instructions. The first is via the semicolon character (;) and the second is via a line break.

See also  phpMyAdmin

For this reason, Javascript statements do not need to end in a semicolon unless we place two statements on the same line.

It is not a bad idea, in any case, to get used to using the semicolon after each statement, since other languages ​​such as Java or C force us to use them and we will be getting used to using a syntax more similar to the usual one in advanced programming environments.

In the next article we will start to talk about the creation of . Also, if you like to learn on video in you also have a .

Loading Facebook Comments ...
Loading Disqus Comments ...