Quick Intro to Javascript Basic Syntax with Examples
Whether you're in any programming forums, groups, or even meme communities, you must have heard of "JavaScript". Yeah, that's right :D The language that without it, the web wouldn't be what it is today. In This Article we'll cover up the basic syntax of JavaScript Language.
Photo by Digital Buggu on Pexels
Before beginning this course you need to have an environment to execute JavaScript, either a browser or a server side environment such as node.js
<script> <script> // here write your code
<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.
Snippet :
<script type = " "></script>
Snippet :
<script language = " "> </script>
Snippet :
<script> // Here write your code </script>
External scripts can be embedded with the usage of snippet :
<script src="X/Y.js"></script>
<script>
tag multiple times, one
<script>
Snippet :
<script src="#"></script> <script src="#"></script> <script src="#"></script>
etc ...
5# Ignoring The Code :
If the src attribute is set, any code inside the
<script>
tag will be ignored and not executed. → either use internal or external source at once (cannot be nested).
Snippet :
<script src="script.js"> alert('Hello World'); // will be ignored; </script>
<script> alert ('Hello World'); // notice the semicolon at the end of the statement (recommended to always use for beginners ) </script>
<script> // something goes here OR alert('Hello World'); // something can always go here </script>
<script> /* alert('Hello World'); alert('5 + 5 + 5'); */ </script>
8# Using The Modern JavaScript:
' use strict ' or " use strict " is inserted at the beginning of a file to ensure that the script will work with the modern JavaScript way. The use of classes and modules does not need the " use strict " and once you are in the strict mode, you cannot go back.
9# Variables :
-
Variables are value containers. In JavaScript, we declare a variable with a "let " keyword following by the variable name;
snippet :<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.
Snippet :<script> let name ; name = 'Drake'; </script>
-
We can merge the declaration and the assignment in the same statement.
Snippet :<script> let name = 'Drake'; </script>
-
To access the variable value just call it by its name.
Snippet :script> let name = 'Drake"; alert (Drake) ;
-
Declaring multiple variable is possible in the same line or it can be done in multi lines.
Snippet :<script> let name = 'Drake' , Age = 25 ; // or let name = 'Drake'; let age = 25; </script>