Author:TAM, JJ [TAM, JJ]
No description
Tags
Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
LEARN JAVASCRIPT QUICKLY AND JAVASCRIPT CODING EXERCISES CODING FOR BEGINNERS WITH HANDS ON PROJECTS BY J J TAM
Page
3
Introduction to Javascript Javascript: Hello world program Javascript: Variables Javascript: Statements Javascript: comments JavaScript: Types Javascript: operators Javascript: Arithmetic Operators Javascript: Increment/decrement: ++, -- operators Javascript: Bitwise operators JavaScript: Logical operators JavaScript: Assignment operators JavaScript: Compound Assignment operator Javascript: Ternary operator JavaScript: Working with boolean values JavaScript: Decision Making: if, if-then Javascript: if-else-if-else ladder JavaScript: Decision making using switch statement JavaScript: loops
Page
4
JavaScript: do-while loop JavaScript: for loop JavaScript: break statement JavaScript: continue statement JavaScript: break in label form JavaScript: for in loop JavaScript: for-of loop JavaScript: get the type of variable JavaScript: Working with strings JavaScript: Nested functions JavaScript: Array JavaScript: Introduction to Classes JavaScript: Define class using class expressions JAVASCRIPT CODING EXERCISES Display the current day and time Print the contents Display the current date Find the area of a triangle Check whether a given year is a leap year
Page
5
Calculate multiplication and division Convert temperatures Find the largest Reverse a given string Replace every character Capitalize The First Letter Convert number to hours and minutes Count vowels in a given string Create a new string Concatenate two strings Move last three character Compute the sum Add two digits Check whether a given year is a leap Check given positive number Check a string starts with 'Java' Check two given integer values Find a value which is nearest to 100
Page
6
Introduction to Javascript Javascript is a scripting language, used to validate html form data, before submitting to the server. Now a days javaScript is also used for server side development (Read node.js) What can I do with Javascript? a. You can perform client side validation. b. You can give dynamic behavior to static HTML pages. c. Show animations on web page d. You can update partial portion of page using AJAX. e. Develop Server side applications using frameworks like node.js Javascript is interpreted language Javascript is interpreted language, write once and attach it to HTML page. No further compilation is required. Is there any relation between JavaScript and Java? Absolutely there is no relation between JavaScript and Java. What is ECMAScript? ECMAScript is a standard, JavaScript is the language that implement ECMAScript standard. Is JavaScript supports Unicode? JavaScript programs are written in Unicode. ECMAScript 5 standard supports Unicode 3. Is JavaScript case-sensitive? Yes JavaScript is a case sensitive language.
Page
7
Is it compulsory to end statement using ;? Many languages like Java, C, C++ use ; as statement terminator. Usage of ; is optional in JavaScript, but I always prefer to use ; to maintain consistency. Is JavaScript support automatic garbage collection? Yes, JavaScript support automatic garbage collection, whenever JavaScript interpreter finds an unreachable object (or) variable, it frees the memory allocated for that variable (or) object. Is JavaScript an object oriented language? Yes, JavaScript is an object-oriented language, but the kind of inheritance it provides is different from the inheritance provided by languages like Java and C++. JavaScript supports Prototype inheritance.
Page
8
Javascript: Hello world program helloWorld.html <!DOCTYPE html> <html> <head> <title>Hello world</title> </head> <body> <script type="text/javascript"> document.write("Hello World"); </script> </body> </html> document.write("Hello World"); Above statement gives instruction to the browser to write "Hello World". Java Script is embedded between <script></script> tags.
Page
9
Javascript: Variables Variable holds a value. Following are some of the examples of variables. var name = "Krishna" var id=123 Above statements define two variables name, id. ‘var’ keyword is used to define variables. Naming constraints a. Variable name must begin with any alphabet, _ (or) $. b. Variable name can contain any character. c. Spaces are not allowed in between variable names. d. Don’t use Javascript keywords as variable names. e. Variable names are case sensitive. Name, NAme, name are 3 different variables. variables.html <!DOCTYPE html> <html> <head> <title>Variables</title> </head> <body> <script type="text/javascript"> var name = "Krishna" ;
Page
10
var id=123; document.write("name = " + name + "<br />"); document.write("id = " + id); </script> </body> </html> Note Java script is case sensitive, so the names banana, Banana, BaNaNa are three different variables.
Page
11
Javascript: Statements Statement is an executable statement, statements in Javascript are terminated using ‘;’. statements.html <!DOCTYPE html> <html> <head> <title>Variables</title> </head> <body> <script type="text/javascript"> document.write("Hello World" + "<br />"); document.write("Hello PTR"); </script> </body> </html> In the above example, below are the java script statements. document.write("Hello World" + "<br />"); document.write("Hello PTR");
Page
12
Javascript: comments Comments are used to document the source code. Comments in javaScript, makes the program more readable. Comments are ignored by JavaScript. Suppose you written thousand lines of program, with out proper documentation, after some time, for the owner of the application also, it is very difficult to figure out what was written. Comments solve this problem: By using comments, you can document your code at the time of writing program. Comments won't affect your program execution. Comments simply document your code. Javascript support 2 kinds of comments. a. Single line comments b. Multi line comments Single line comment : Single line comments starts with // // It is a single line comment Multi Line comment Multi line comments are encoded in /* */ /* I am a mulltiline comment */ sample.html <!DOCTYPE html>
Page
13
<html> <head> <title>Variables</title> </head> <body> <script type="text/javascript"> /* Java script is weakly typed language Author: Hari krishna */ var a = 10; document.write("Value of a is " + a + "<br />"); a = "Hello World"; // Assigning string document.write("Value of a is " + a + "<br />"); a = true; // Assigning boolean document.write("Value of a is " + a + "<br />"); a = 10.09; // Assigning number document.write("Value of a is " + a + "<br />"); </script> </body> </html>
Page
14
JavaScript: Types Types are used to represent the type of data. Primarily, there are two categories of types. a. Primitive types b. Reference types Primitive Types Numbers, strings and Boolean values considered as primitive types. In addition to these, special values null, undefined are also considered as primitive types. Reference Types Objects and arrays are considered as reference types. An object is a collection of name and value pairs. Array is a collection of elements. One more thing to note is that JavaScript treats functions also objects. types.html <!DOCTYPE html> <html> <head> <title>Data Types</title> </head> <body> <script >
Page
15
var a = 10; var b = 10.01; var c = true; var d = "Hello World"; var e = { name : "Hari krishna", city : "Bangalore" }; var f = [2, 3, 5, 7]; document.write("a = " + a + " Type of a = " + (typeof a) + "<br />"); document.write("b = " + b + " Type of b = " + (typeof b) + "<br />"); document.write("c = " + c + " Type of c = " + (typeof c) + "<br />"); document.write("d = " + d + " Type of d = " + (typeof d) + "<br />"); document.write("e = " + e + " Type of e = " + (typeof e) + "<br />"); document.write("f = " + f + " Type of f = " + (typeof f) + "<br />"); </script> </body> </html> Open above page in browser, you can able to see following
Page
16
messages in the html page. a = 10 Type of a = number b = 10.01 Type of b = number c = true Type of c = boolean d = Hello World Type of d = string e = [object Object] Type of e = object f = 2,3,5,7 Type of f = object As you see the output, JavaScript treats both integer and float values as type number and arrays, objects are treated as type object.
Page
17
Javascript: operators An operator is a symbol which perform an operation An operator is said to be unary, if it performs operation on single operand. An operator is said to be binary, if it performs operation on two operands An Operator is said to be ternary if it perform operation on three operands. Javascript provides following operators. a . Arithmetic operators b . Increment/decrement:++, -- c . Bitwise operators d . Logical operators e . Assignment operators f . String concatenation g . Comparison operators h. Other operators like === (Strict comparison), ternary (?:)
Page
18
Javascript: Arithmetic Operators Following table summarizes Arithmetic operators supported by Javascript. Operator Description + Additon (also used for String concatenation) - Subtraction / Division * Multiplication % Gives Remainder arithmetic.html <!DOCTYPE html> <html> <head> <title> Arithmetic Operators </title> <style> p { font-size : 2em ; color : seagreen ; } </style> </head> <body> <p> <script type= "text/javascript" > var a = 23 ; var b = 3 ;
Page
19
document .write( "a = " + a + "<br />" ); document .write( "b = " + b + "<br />" ); document .write( "a+b = " + (a + b) + "<br />" ); document .write( "a-b = " + (a - b) + "<br />" ); document .write( "a*b = " + (a * b) + "<br />" ); document .write( "a/b = " + (a / b) + "<br />" ); document .write( "a%b = " + (a % b) + "<br />" ); </script> </p> </body> </html> Note Unlike languages like Java, integer division in JavaScript returns float value.
Page
20
Javascript: Increment/decrement: ++, -- operators Increment & decrement operators ++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 Pre increment Syntax: ++variable pre increment operator increments the variable value by 1 immediately. Post increment Syntax: variable++ post increment operator increments the variable value by 1 after executing the current statement. Pre Decrement Syntax: --variable pre decrement operator decrements the variable value by 1 immediately. Post Decrement Syntax: variable--
Comments 0
Loading comments...
Reply to Comment
Edit Comment