Photo by Digital Buggu on Pexels
1# Script Insertion:
Insertion of scripts is made with the following tag :
<script> Snippet : <script> // here write your code
snippet :<script> // here write your code script>
There are some indications to spot old JS code which are :
The existence of a (type = " ") or ( language = " " ) attribute which are no longer needed in the modern JS.
<script type = " "></script>
<script language = " "> </script>
<script> // Here write your code </script>
3# Loading From External Storage:
If your code is way too long, you need to load the script from an external source, an independent file with the '.js' extension. Doing this has benefits like less page loading time in addition to traffic and bandwidth usage reduction since the browser will do its task and cache the file and start to load it from the local storage instead of an external storage.
4# Embedding External Scripts:
External scripts can be embedded with the usage of snippet :
(X : absolute path; Y :file name). The src attribute value can be a URL of the file we want to embed which can be placed on any other place on the internet as long as we have access to read that file.If we have several JS files we have to use the
tag multiple times, one
tag for each file.
<script src="#"></script> <script src="#"></script> <script src="#"></script>
5# Ignoring The Code :
If the src attribute is set, any code inside the
tag will be ignored and not executed. → either use internal or external source at once (cannot be nested).
<script src="script.js"> alert('Hello World'); // will be ignored; </script>
6# Semicolons :
Statements are commands that performs actions. They are usually separated with a semicolon ( ; ). JS supports semicolon auto insertion which is a risky thing to do (sometimes causes errors). Going with the "ending statements with a semicolon" is always the recommended option.
<script> alert ('Hello World'); // notice the semicolon at the end of the statement (recommended to always use for beginners ) </script>
Comments are used to describe what the code does and why. Basically there are 2 types of comments:
- Online comments : they start by a double forward slash ( // ). Anything written after that is ignored and will not be executed. Besides, online comments can be set on a new line as they can take the rest of the line after a statement.
<script> // something goes here OR alert('Hello World'); // something can always go here </script>
- Multiline comments : they start by a (/) and ends with a (/) and they are mainly used to disable a block of code for the sake of debugging.
<script> /* alert('Hello World'); alert('5 + 5 + 5'); */ </script>
9# Variables :
<script> let name ; </script>
Assigning a value to the variable can be made with calling the variable by its name and using the equal sign which is actually used for assigning ( = ) then the value and a semicolon.
<script> let name ; name = 'Drake'; </script>
We can merge the declaration and the assignment in the same statement.
<script> let name = 'Drake'; </script>
To access the variable value just call it by its name.
script> let name = 'Drake"; alert (Drake) ;
Declaring multiple variable is possible in the same line or it can be done in multi lines.
<script> let name = 'Drake' , Age = 25 ; // or let name = 'Drake'; let age = 25; </script>