JavaScript is a programming language commonly used in web development. It was originally developed by Netscape as a means to add dynamic and interactive elements to websites. While JavaScript is influenced by Java, the syntax is more similar to C and is based on ECMAScript, a scripting language developed by Sun Microsystems.
JavaScript is a client-side scripting language, which means the source code is processed by the client's web browser rather than on the web server. This means JavaScript functions can run after a webpage has loaded without communicating with the server. For example, a JavaScript function may check a web form before it is submitted to make sure all the required fields have been filled out. The JavaScript code can produce an error message before any information is actually transmitted to the server.
Like server-side scripting languages, such as PHP and ASP, JavaScript code can be inserted anywhere within the HTML of a webpage. However, only the output of the server-side code is displayed in the HTML, while JavaScript code remains fully visible in the source of the webpage. It can also be referenced in a separate, JS file, which may also be viewed in a browser.
JavaScript functions can be called within <script> tags or when specific events take place. While standard JavaScript is still used for performing basic client-side functions, many web developers now prefer to use JavaScript libraries like jQuery to add more advanced dynamic elements to websites.
Advantage and disadvantage of javascript
Advantages of JavaScript
- Speed. Client-side JavaScript is very fast because it can be run immediately within the client-side browser. Unless outside resources are required, JavaScript is unhindered by network calls to a backend server. It also has no need to be compiled on the client side which gives it certain speed advantages (granted, adding some risk dependent on that quality of the code developed).
- Simplicity. JavaScript is relatively simple to learn and implement.
- Popularity. JavaScript is used everywhere in the web. The resources to learn JavaScript are numerous. StackOverflow and GitHub have many projects that are using Javascript and the language as a whole has gained a lot of traction in the industry in recent years especially.
- Interoperability. JavaScript plays nicely with other languages and can be used in a huge variety of applications. Unlike PHP or SSI scripts, JavaScript can be inserted into any web page regardless of the file extension. JavaScript can also be used inside scripts written in other languages such as Perl and PHP.
- Server Load. Being client-side reduces the demand on the website server.
- Extended Functionality. Third party add-ons like Greasemonkey enable JavaScript developers to write snippets of JavaScript which can execute on desired web pages to extend its functionality.
- Versatility. Nowadays, there are many ways to use JavaScript through Node.js servers. If you were to bootstrap node.js with Express, use a document database like mongodb, and use JavaScript on the front-end for clients, it is possible to develop an entire JavaScript app from front to back using only JavaScript.
- Updates. Since the advent of EcmaScript 5 (the scripting specification that Javascript relies on), Ecma International has dedicated to updating JavaScript annually. So far, we have received browser support for ES6 in 2017 and look forward to ES7 being supported in future months.
Disadvantages of JavaScript
- Client-Side Security. Because the code executes on the users’ computer, in some cases it can be exploited for malicious purposes. This is one reason some people choose to disable Javascript.
- Browser Support. JavaScript is sometimes interpreted differently by different browsers. Whereas server-side scripts will always produce the same output, client-side scripts can be a little unpredictable. Don’t be overly concerned by this though - as long as you test your script in all the major browsers you should be safe. Also, there are services out there that will allow you to test your code automatically on check-in of an update to make sure all browsers support your code.
Basic concepts of javascript
Let's explain some of the core features of the JavaScript language, to give you a better understanding of how it all works.
Variables
Variables are containers that you can store values in. You start by declaring a variable with the
var keyword, followed by any name you want to call it:
var myVariable;
Comments
You can put comments into JavaScript code, just as you can in CSS:
/*
Everything in between is a comment.
*/
If your comment contains no line breaks, it's often easier to put it behind two slashes like this:
// This is a comment
Operators
An operator is a mathematical symbol which produces a result based on two values (or variables). In the following table, you can see some of the simplest operators, along with some examples to try out in the JavaScript console.
There are some more operators to be explored in javascripts.
Operator
|
Explanation
|
Symbol(s)
|
Example
|
Addition
|
Used to add two numbers together or glue two strings together.
|
+
|
6 + 9;
"Hello " + "world!";
|
Subtraction, Multiplication, Division
|
These do what you'd expect them to do in basic math.
|
-, *, /
|
9 - 3;
8 * 2; // multiply in JS is an asterisk
9 / 3;
|
Assignment
|
You've seen this already: it assigns a value to a variable.
|
=
|
var myVariable = 'Bob';
|
Equality
|
Does a test to see if two values are equal to one another and return a true/false (Boolean) result.
|
===
|
var myVariable = 3;
myVariable === 4;
|
Not, Does-not-equal
|
Returns the logically opposite value of what it precedes; it turns a true into a false, etc. When it is used alongside the Equality operator, the negation operator tests whether two values are not equal.
|
!, !==
|
The basic expression is true, but the comparison returns false because we've negated it:
var myVariable = 3;
!(myVariable === 3);
Here we are testing "is myVariable NOT equal to 3". This returns false because myVariable IS equal to 3.
var myVariable = 3;
myVariable !== 3;
|
There are some more operators to be explored in javascripts.
Conditions
Conditionals are code structures which allow you to test if an expression returns true or not, running alternative code revealed by its result. A very common form of conditionals is the
if
... else statement.
for example:
var x = '1000';
if (x === '1000') {
alert('true');
} else {
alert('false');
}
Functions
Functions are a way of packaging functionality that you wish to reuse. When you need the procedure you can call a function, with the function name, instead of rewriting the entire code each time.
Document.getelementbyid(), alert, etc are the inbuilt functions.
for example:
function multiply(num1,num2) {
var result = num1 * num2;
return result;
}
Events
Real interactivity on a website needs events. These are code structures which listen for things happening in the browser, running code in response. The most obvious example is the click event, which is fired by the browser when you click on something with your mouse.
for example:
document.querySelector('html').onclick = function() {
alert('Ouch! Stop poking me!');
}










