Introduction to Cheat Sheet JavaScrip
JavaScript is a client-side as well as a server-side scripting language that can be inserted into HTML pages and is interpreted by web browsers. JavaScript is an Object-based Programming language. It is developed by Netscape. JavaScript is lightweight and cross-platform. JavaScript is not a compiled language, but it is a translated language. The JavaScript Translator embedded in the browser which is responsible for translating the JavaScript code for the web browser. JavaScript is used in client-side validation, used to create an interactive website, in Today’s world lots of JavaScript frameworks present. Which helps to build more attractive websites, JavaScript improves web applications performance by doing the work at the client-side. It does validation at the client-side then redirects to a server, in this way server does not need to deal with user’s invalid inputs data. JavaScript is used in event handling like clicking the button, mouse hover, mouse drag, mouse drop, mouse in, mouse out. We can load on a body of the HTML page by the help of JavaScript by using unload method, onload method gets called on loading of a web page on this event we can call the JavaScript function to improve the performance. JavaScript does not require expensive tools to do development.
Commands and Content on Cheat Sheet JavaScript
JavaScript has written inside a script tag, which may present anywhere in an HTML web page but it is recommended to use this tag inside head tag of HTML.
Example.
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
document.write("Hello World! Test")
//-->
</script>
</body>
</html>
Script tag has two attributes “language ” and type.
Language tells which language we are going to use and type for a scripting language.
Above program print output – Hello World! Test
Javascript ignores new lines, spaces, tabs. A semicolon is optional in javascript language, it will not throw compile time error like another language in case of absence of semicolon. JavaScript is a case-sensitive language. It means that the language variables, keywords, function names, and identifiers must always be typed with a consistent capitalization of letters.
Comments in JavaScript
Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript. Any text between the characters /* and */ is treated as a comment. This may span multiple lines.JavaScript also recognizes the HTML comment opening sequence <!–. JavaScript treats this as a single-line comment, just as it does the // comment. The HTML comment closing sequence –> is not recognized by JavaScript so it should be written as //–>.
<script language="javascript" type="text/javascript">
<!—type1
// type2
/*
* type3
*/
//--> typ4
</script>
1. How to write javascript in head tag?
Code:
<html>
<head>
<script type="text/javascript">
<!--
function sayHi() {
alert("Welcome World")
}
//-->
</script>
</head>
<body>
<input type="button" onclick="sayHi()" value="Say Hello" />
</body>
</html>
In the above example on click on a button, it will give an alert box with Welcome World.
2. Writing javascript in a body section
Code:
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--
document.write("Test demo")
//-->
</script>
<p>This is web page body </p>
</body>
</html>
Way to include external js file in an HTML page.
<html>
<head>
<script type="text/javascript" src="filename.js" ></script>
</head>
<body>
</body>
</html>
3. JavaScript Datatypes
Javascript supports 3 primitive data types which are following.
- Number
- Boolean
- String
Along with it defines trivial data type null, undefined and support composite data type also which is known as an object.
4. JavaScript Variables
Variable is the name of the given memory location, Eg.
<script type="text/javascript">
<!--
var money;
var name;
//-->
</script>
Javascript reserved keywords
- abstract else instanceof switch boolean enum int synchronized
- break export interface this byte extends long throw
- case FALSE native throws catch final new transient
- char finally null TRUE class float package try
- const for private typeof continue function protected var
- debugger goto public void default if return volatile
- delete implements short while do import static with
- double in super
5. Javascript operator
It supports the following operator.
- Arithmetic Operators
- Logical (or Relational) Operators
- Comparison Operators
- Assignment Operators
- Conditional (or ternary) Operators
6. Loops in Javascript
Javascript supports the following loops
for loop, while loop, for in loop.
Free Tips and Tricks of using JavaScript Commands
- Javascript mostly used for client-side validation like textbox validation so that user may enter correct data to a textbox and many more, it always best to write javascript in an external file and include in HTML page
- null ,undefined,0, false, NaN, ” (empty string) are all false.
- How to check given argument is number
function isNumberTest(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
- Getting min and max for given array
var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var max = Math.max.apply(Math, numbers);
var min = Math.min.apply(Math, numbers);
- how to use map() function method to loop through an array’s items
var squares = [1,2,3,4,5].map(function (val) {
return val * val;
});
// squares will be equal to [1, 4, 9, 16,25]
How to use comma operator in javascript
var a = 0;
var b = ( a++, 120 );
console.log(a); // a will be equal to 1
console.log(b); // b is equal to 120
- Avoid using for-in loop for arrays, with(),eval() or the Function constructor, negative indexes in arrays.
- Use a switch/case statement instead of a series of if/else, use switch/case statement with numeric ranges.
Conclusion
As we saw how much powerful javascript, We can use this language on our website to make it more attractive and scalable. It provides front-end validation which helps to improve the performance of web application else we have to submit the form and validation take place at ser server which Server-side which will cost to performance. Today world many cheat sheet javascript frameworks present which can make our website more attractive and powerful. It means we can use cheat sheet javascript as front-side scripting language whenever our project is needed.