This document provides an overview of JavaScript including:
- It is a dynamic programming language that is interpreted and has object-oriented capabilities. It was introduced in Netscape 2.0 in 1995.
- It allows for increased interactivity on webpages through features like validating user input without page reloads and creating rich interfaces.
- It has various data types, variables, operators, and control structures like conditionals and loops.
- It uses objects and object-oriented programming. Common built-in objects include numbers, strings, arrays, dates and more.
- Functions are reusable blocks of code that can be defined and called. Events allow JavaScript to interact with HTML. Regular expressions provide pattern matching capabilities.
2. JavaScript is a dynamic computer programming
language.
It is an interpreted programming language with
object-oriented capabilities.
3. JavaScript was first known as LiveScript.
JavaScript made its first appearance in Netscape
2.0 in 1995
The general-purpose core of the language has
been embedded in Netscape, Internet Explorer
and other web browsers.
4. Less server interaction: You can validate user input before sending the
page off to the server. This saves server traffic, which means less load
on your server.
Immediate feedback to the visitors: They don't have to wait for a page
reload to see if they have forgotten to enter something.
Increased interactivity: You can create interfaces that react when the
user hovers over them with a mouse or activates them via the keyboard.
Richer interfaces: You can use JavaScript to include such items as
drag-and-drop components and sliders to give a Rich Interface to your
site visitors.
6. Script in <head>...</head> section.
Script in <body>...</body> section.
Script in <body>...</body> and <head>...</head>
sections.
Script in an external file and then include in
<head>...</head> section.
7. Three primitive data types:
Numbers
Strings
Boolean
Undefined
null
8. A variable is named which is used to refer the data.
Variables are declared with the var keyword.
Example:
var name=“Govardhan”
Note: The value type of a variable can be change during the execution of a
program.
9. Variable Names:
Variable name contains alphabets, digits or underscore(_). But
it should not start with a digit.
You should not use JavaScript reserved keywords as variable
name.
JavaScript variable names are case-sensitive.
Scope:
Global variables
Local variables
10. abstract debugger final instanceof public transient
boolean default finally int return true
break delete float interfacce short try
byte do for long static typeof
case double function native super var
catch else goto new switch void
char enum if null synchronized volatile
class export implements package this while
const extends import private throw with
continue false in protected throws
12. if Statement
if (expression){
/*Statement(s) to be executed if
expression is true*/
}
if…else Statement
if (expression){
/*Statement(s) to be executed if
expression is true*/
}else{
/*Statement(s) to be executed if
expression is false*/
}
13. if…else if Statement
if (expression 1){
/*Statement(s) to be executed if
expression 1 is true*/
}else if (expression 2){
/*Statement(s) to be executed if
expression 2 is true*/
}else if (expression 3){
/*Statement(s) to be executed if
expression 3 is true*/
}else{
/*Statement(s) to be executed if no
expression is true*/
}
switch Statement
switch (expression){
case condition 1:
statement(s)
break;
case condition 2:
statement(s)
break;
...
case condition n:
statement(s)
break;
default: statement(s)
}
15. for Statement
for (initialization; test condition; iteration statement){
/*Statement(s) to be executed if test condition is true*/
}
for…in Statement
for (variablename in object){
/*Statement or block to execute*/
}
for…of Statement
for (variablename of object){
/*Statement or block to execute*/
}
17. JavaScript is an Object-Oriented Programming
Language.
JavaScript objects are collections of properties i.e.
data or method.
JavaScript objects are accessed through
references.
18. Object Data:
objectName.objectProperty = propertyValue;
Object Methods:
objectName.methodName(arguments);
User-Defined Objects:
var objectName = new Object([parameters]);
20. The Array object lets you store multiple values in a single
variable.
Array Method:
array1.concat(array2);
array.indexOf(searchElement[, fromIndex]);
array.join(separator);
array.push(element1, ..., elementN);
array.pop();
array.reverse();
array.sort( compareFunction );
21. A function is a group of reusable code which can
be called anywhere in your program.
Syntax:
function functionName(parametersList){
/*Statement(s)*/
return ;
}
22. From version 1.2 onwards JavaScript allows function
definitions to be nested within other functions as well.
Syntax:
function outsideFunctionName(ParametersList){
//Outside funciton block
function insideFunctionName(ParametersList){
//Inside function block
}
}
23. You can also define function dynamically using
Function() constructor.
The unnamed functions created with the
Function() constructor are called anonymous
functions.
Syntax:
var variablename = new Function(arg1,arg2…, FunctionBody);
24. JavaScript 1.2 introduces the concept of
function literals which is another new way of
defining unnamed functions.
Syntax:
var variablename = function(ArgumentList){
Function Body
};
25. JavaScript's interaction with HTML is handled through events that
occur when the user or the browser manipulates a page.
The following are some of HTML5 events:
onload
onclick
ondblclick
onmouseover
onmouseout
onchange
26. The JavaScript RegExp class represents regular expressions, and
both String and RegExp define methods that use regular
expressions to perform powerful pattern-matching and search-
and-replace functions on text.
Syntax:
var pattern = new RegExp(pattern, attributes);
(or)
var pattern = /pattern/attributes;
27. TOKEN DESCRIPTION
^ It matches any string at the beginning
$ It matches any string at the end
+ It matches any string containing one or more
* It matches any string containing zero or more
? It matches any string containing at most one
{n} It matches any string containing a sequence of N
[…] Any one character between the brackets
[^…] Any one character not between the brackets
| matches any of the alternatives specified
28. TOKEN DESCRIPTION
s Matches a whitespace character (space, tab, newline)
S Matches non-whitespace character
d Matches a digit (0-9)
D Matches a non-digit
w Matches a word character (a-z, A-Z, 0-9, _)
W Matches a non-word character
29. Modifier DESCRIPTION
g Performs a global matchthat is, find all matches rather than stopping
after the first match.
i Perform case-insensitive matching.
m Specifies that if the string has newline or carriage return characters,
the ^ and $ operators will now match against a newline boundary,
instead of a string boundary
30. METHOD DESCRIPTION SYNTAX
match()
Searches a string for a match against a
regular expression, and returns the
matches, as an Array object.
string.match(regexp)
exec()
Executes a search for a match in its string
parameter.
RegExpObject.exec( string );
test() Tests for a match in its string parameter. RegExpObject.test( string );
toSource()
Returns an object literal representing the
specified object; you can use this value to
create a new object.
RegExpObject.toSource();
toString()
Returns a string representing the specified
object.
RegExpObject.toString();