Javascript Introduction
Javascript Introduction
JavaScript Applications
JavaScript is the widely used programming language, all over the world. It has the
largest open-source package repository in the world (npm). Every type of software
uses JavaScript, including the server code (Node.js), productivity apps, 3D games,
robots, IoT devices. JavaScript has achieved the goal, set by Java a long time ago:
write once, run anywhere. There are various JavaScript uses in different segments.
JavaScript Facts
JavaScript History
WWW was formed in 1990. Initially, it was a bunch of web-pages linked together. But
soon people want more interactive websites. So on-demand of Netscape, Brenden Eich,
(inventor of JavaScript) in 1995 invented a prototype based (Classless) language
for their Navigator Browser. Initially, it was called "LiveScript", but later on
renamed as " JavaScript ".
JavaScript Engines
JavaScript Engines are the computer programs used to interpret JavaScript into
machine code. JavaScript was primarily developed for browser environment only, but
non-browser environments are also using JavaScript now, like Node JS, and Deno.
There are so many JavaScript engines available, but the most popular JavaScript
Engine is Chrome V8 is open source and the most popular JavaScript Engine. Being
the fastest JavaScript Engine, a non-browser environment like Node JS is also using
Chrome V8 Engine. SpiderMonkey is the First JavaScript Engine developed by Brendan
Eich at Netscape. It is currently maintained by Mozilla Foundation.
<script>
document.write('Hello Javascript');
</script>
<script src="custom.js"></script>
JavaScript can be placed on both Head or Body tag of our HTML Page using <script>
tag. When Webpage loads, script code executes and can slow down page speed.
Write JavaScript coding in head tag only when we want script to execute first, like
to disable text selection, page redirection, notifications etc. Rest all script
like JQuery, Angular JS or custom JS should be written just before body closing
tag. This can load DOM content first, then scripts will execute, and hence optimize
webpage performance.
<!DOCTYPE html>
<html>
<head>
<title>Javascript</title>
<meta charset="utf-8" />
<script>
// Write Script here
</script>
</head>
<body>
// body content
</body>
</html>
Run JavaScript Code in Body
<!DOCTYPE html>
<html>
<head>
<title>Javascript</title>
<meta charset="utf-8" />
</head>
<body>
// content in body
<script>
// Write Script here
</script>
</body>
</html>
JS console.log()
To print JavaScript output in console window of web browser, use JavaScript and use
console.log() function. console is a JavaScript Object and log() is a Function of
console object. Any syntax error in JavaScript is shown in console window. Also
global variables and functions are accessible in console.
Console Shortcuts
Browser
Chrome
Ctrl + Alt + j
Cmd + Alt + j.
Firefox
Ctrl + Shift + k
Cmd + Alt + k
Internet Explorer
Ctrl + 2 or F12
NA
Edge
Ctrl + 2 or F12
Cmd + Alt + j
Safari
Ctrl + Alt + c
Cmd + Alt + c
hello string
<script>
var x = 'hello string';
console.log(x);
</script>
error found
<script>
console.error('error found');
</script>
To clear console, use console.clear()
JavaScript supports three dialog box. These dialog boxes are build in functions of
window object. Three dialog box in JavaScript are alert, prompt and confirm.
JavaScript Alert, alert(): Alert box, i.e alert() or window.alert() is used to show
output in dialog box. For alerts, use alert(). Alert generally block the code, thus
next code block will run only after alert is closed.
<script>
var x = 'hello js';
alert(x);
</script>
<script>
var x = prompt('Enter Name');
alert(x);
</script>
<script>
var x = confirm('Press Ok or Cancel');
alert(x);
</script>
JavaScript Comments
Comments are used to write explanations, hints, and to stop execution of code.
JavaScript use two comments syntax. One is Single line comment, and second is
Multi-line comment.
Single Line Comment in JavaScript: Single Line comments in JavaScript are used
using //. This will comment only right hand side of code.
Example
<script>
// single line comment
</script>
Multiline Comment in JavaScript: JavaScript Multiline Comments are recommended
comments as they have opening and closing. Multiline Comments are used using /*
opening and */ closing, same like css comments.
Example
<script>
/* Multiline Comment */
</script>
Always prefer Multiline comments in JavaScript for production. Single line comments
in JavaScript are good for development purpose. But in production, they can create
errors after JS Minification.
Noscript Tag
<noscript> tag is an html element used when JavaScript is disabled in web browser.
If JavaScript is enable, noscript tag is invisible.
Example
<noscript>
Please Enable Javascript
</noscript>
In HTML5, type and language attributes from script tag has been deprecated. So we
can write JavaScript code directly in script tag without any attribute.