How Can We Help?
Introduction
In HTML documents and webpages JavaScript can be included in two ways. One way is to include the JavaScript is internally known as the Internal method. Using the inline method we can write the JavaScript code in between the <script> element. The other way to include the JavaScript code is by using the external method. Using the external method the JavaScript code is present in a separate external document and we need to reference it in the HTML document using the src attribute of the <script> element.
For example:
<script src=”includeExternalFile.js”>.
The script tag has many attributes: –
- defer : It is used to specify that the script is executed only when the page has finished parsing.
- async : The async attribute is used to execute the script asynchronously.
- src : It is used to specify the URL of the external script file. It is generally used to include the external script file in an HTML document.
- type : It is used to specify the type of the script file.
- language : It is used to specify the type of scripting language we are using in an HTML document. In our case it is JavaScript.
For including the script file in our HTML document we mainly use the language and type attribute of the <script> tag. An example is demonstrated below to show it :
<script language=”javascript” type=”text/javascript”>
console.log(“Hello World”); //prints Hello World in the console
</script>
Using Semicolons in JavaScript is Optional
In languages like C, C++, Java using semicolons are mandatory but in a language like JavaScript if you write each piece of code in a different line then it is not mandatory to use a semicolon to separate each line of code.
For example :
<script language="javascript" type="text/javascript">
var firstName = "Joe"
var lastName = "Doe"
var fullName = firstName.concat(lastName)
</script>
Comments in JavaScript
Comments are an efficient way to make the code readable in Javascript. It can also be used when we want to execute an alternative code. Javascript comments are of two types :
- Single-Line Comments: Any text in between // and the end of the line is always ignored by JavaScript.
- Multi-line Comments: Multi line Comments start with /* and end with */. Any text in between these two /* and */ will never be executed by JavaScript.
For Example :
<script language = "javascript" type = "text/javascript">
// This is a single-line comment.
/*
This is a multi-line comment.
*/
</script>
Case Sensitivity in JavaScript Language
In JavaScript lower case and upper case, letters are treated differently. This means that the keywords, variables, and function names must be typed with a consistent capitalization of letters.
“WHILE” and “while” are treated totally differently in JavaScript.
We need to take proper care while writing the variable names, keywords, identifiers, function names, etc in JavaScript.
White spaces and Line Breaks are Ignored in JavaScript
JavaScript ignores indentations, tabs, line breaks so that a programmer can write a readable code as he wants without any hassle of remembering where to use indentations in JavaScript. As a programmer you can use tabs, spaces and line break to make your code look more professional and neat for you and other developers to understand.
In this tutorial, we have explained basic JavaScript syntax.
If you have an inquiry or doubt don’t hesitate to leave them in comment. we are waiting your feedback.But remember to understand the concept very well, you need to practice more.
Hopefully, it was clear and concise.
JavaScript tutorials list:
- JavaScript tutorials list : access all JavaScript tutorials.