Javascript (also known as JS) is the fastest-growing Programming language according to the PYPL index. Java is used almost everywhere from Mobile to websites.
Learning JavaScript is not so difficult.
So, in this post, I am going to explain the basics of Javascript or JS.
What is Javascript?
JavaScript (also known as JS) is a programming language. Javascript is used to develop web applications and somehow in mobile applications, etc. Brendan Eich was the creator of JavaScript, and found himself operating for web browsers in 1995. The project manager assigned Brendan a task on the face of it not possible task: to form a programming language that worked within the web browser Navigator browser.
Discovery of Javascript
Brendan Eich formed JavaScript (originally named Mocha), which was developed in exactly ten days. The results of those ten days of development? An odd language, decorated with things that square measure themselves strange. Since that point, JavaScript has become the universal language of the online, though it’s ne’er enjoyed a stellar name.
What is Javascript used for?
As mentioned, JavaScript is a programming language for creating websites and other awesome stuff.
Some of the main uses of JavaScript are as follows:
- Website development
- PWA application development
- Mobile app development
- Desktop app development
- The backend of applications or Servers
- Programming in general and much more
Basics of Javascript
1. Integrate JavaScript into web pages
In-line code
In HTML pages each element is defined by means of special tags with specific functions. The integration of JS codes within HTML pages is possible simply by defining one of these tags in any desired position.
In fact, JavaScript lives inside web pages through the <script>… </script> tags.
These tags can be multiple on the same page, we can also say unlimited.
Furthermore, unlike many other HTML elements, <script> can be inserted in both <body> and <head> without particular distinction. The choice falls on the basics of needs, many scripts in fact need to be executed before rendering or loading the page to work.
In the case of secondary or manual scripts, it is possible (and recommended) to define them in the page footer, in the lowest position, in order to make the page load faster.
External code
Another method for integrating JavaScript code into an HTML page is through external files in .js format Much like CSS, you can call external JavaScript resources on a page like this:
<script type=”text/JavaScript” src=”awc360.js”></script>
The URL or path of the file in .js format must be entered in the “src “attribute. The position of the HTML tag follows the same rules of in-line writing as in the previous section.
NB In the <script> tag it is possible to add an instruction on the format defined by the “type” attribute (type =”text/JavaScript”). In the latest versions of JS, it is more mandatory to indicate this instruction.
Example
Below is an example of opening the JavaScript code in an HTML page, devoid of actual useful code.
<! DOCTYPE html>
<html>
<head>
<script type=”text/javascript”>
….
</script>
</head>
<body>
<script>
….
</script>
<script type=”text/javascript” src=”360customcode.js”></script>
</body>
</html>
2. ECMAScript and Javascript
ECMAScript while learning Javascript you hear this term a lot so it would be great if you understand it properly from the beginning.
ECMAScript is the official name for JavaScript. A new name became necessary because there is a trademark on JavaScript (held originally by Sun, now by Oracle). At the moment, Mozilla is one of the few companies allowed to officially use the name JavaScript because it received a license long ago. For common usage, these rules apply:
JavaScript means the programming language.
ECMAScript is the name used by the language specification. Therefore, whenever referring to versions of the language, people say ECMAScript. The current version of JavaScript is ES2020.
Bonus: ES6 is one of the common terms you hear while learning because ES6 comes with a lot of awesome features that are totally new in JS, so check it when you are ready to learn.
3. JavaScript Variable: Declare, Assign a Value
Variables are containers that contain or store any types of data in the program.
Javascript is a dynamic programming language so it’s not necessary to define the data type of the value you want to store.
Before using variables in the program you have to declare them first.
How to declare a variable in Javascript:
var variablename;
Note: var variablename = “value here”; is not an example of declaring variables.
How to assign value to a variable in Javascript
var variablename = "some value"
// OR
variablename = "some value"
4. How to create objects in Javascript
If you ask me what’s the main element in the whole Javascript I will definitely answer this question with one word “Object”.
In Javascript, almost everything is an object, so it’s properly essential for you to know how to create objects in Javascript.
Ok, let’s do this,
var objName = new Object();
objName.property1 = value1;
objName.property2 = value2;
objName.method1 = function()
{
line of code
}
//OR
var objName= {
property1:value1
property2:value2
method1: function()
{
// lines of code
}
};
Ok, this is enough for now but don’t think it’s over. There is a lot to learn such as Constructor, etc.
5. Naming rules in Javascript for variables, functions, etc.
Though you can name the variables as you like, it is a good programming practice to give descriptive and meaningful names to the variables.
Here are some naming rules in Javascript.
The first character must be a letter or an underscore (_). You can’t use a number.
The rest of the variable name can include any letter, any number, or the underscore. You can’t use any other characters, including spaces, symbols, and punctuation marks.
As with the rest of JavaScript, variable names are case-sensitive.
There’s no limit to the length of the variable name.
You can’t use one of JavaScript’s reserved words as a variable name. All programming languages have a supply of words that are used internally by the language and that can’t be used for variable names because doing so would cause confusion (or worse). Note, too, that JavaScript also has many keywords that should be avoided as well. (for example: for, while, function)
6. JavaScript rules
The JavaScript code to be executed correctly by browsers must respect certain rules; otherwise, it will be easy to run into unpleasant errors.
These rules are easy to remember and we can summarize them in these points:
Each line of code must end with a semicolon (;).
JS is case sensitive, that is, it distinguishes between lowercase and uppercase letters (“Name” is different from “name”.
The custom name of variables objects and functions cannot start with a number or symbol.
The separation of words that make up a custom value/name can be defined with the symbol “_” (underscore).
Furthermore, custom names cannot have spaces between the words that compose them
It is recommended to use names like my Variable or my_function for example.
To define text strings or values, they must be enclosed in single (‘) or double (“) quotes, otherwise, they will not be executed and will return errors.
You cannot give prohibited names to variables or functions. For forbidden we mean those pre-imposed names present in the language that already have established functions such as function, while, for, in, do, return, this, etc …
Keep these concepts in mind but don’t give them too much weight, you will see everything in detail in the following articles.
JavaScript is a typed language but without too many restrictions or constraints, that is, to be executed it does not need an ordered and specific structure, but is able to interpret scripts even with any errors and shortcomings, compensating the absent values with those it deems most appropriate (example ‘absence of a semicolon or repetition of variables).
JavaScript is a language that executes “cascading” instructions, i.e. from first to last in order of writing on the page. Any statement executed after, if required, can overwrite a previous one.
Therefore pay close attention to writing the code and try to be as orderly as possible following the hierarchy of elements.
This was the introduction to JavaScript for the first article of our course, which as mentioned will be for beginners. Also in the next ones, we will try to give detailed explanations without superfluous information, because JS like many other programming languages are based a lot on constant practice.
If you want to learn more, you can take a look at the “HTML & JS functions ” section of our site, you will find many useful tutorials to start programming with JS simply and intuitively.