Skip to main content

Posts

Showing posts with the label javascript

JAVASCRIPT FUNCTIONS

Functions are the block of code that performs specific task and often return a value. Any function can be understood as a reusable code-block that will be executed by an event, or when the function is called. A javaScript function may return zero or more parameters. Parameters can be understood as the standard technique of passing control to a function. JavaScript functions can be of two types: 1. Built-In Function: JavaScript provides a set of in built function that can be used to perform specific functions Some of them are eval(),parseInt(),parseFloat(). a. eval(): eval() function is used to convert a string expression a numeric value E.g. Evaluating a expression using eval() var a=eval(“5*5+6”); eval() function evaluates the expression and assigns 26 to variable a . Note: In case you pass a string value as a parameter eval() expression will not be generated. You need to convert the data type. This can be done using parseInt() discussed below: b. parseInt(): parseInt(

Loops in Javascript

The do...while Loop The do...while loop is just a variant of the while loop. In such loop the block of code is always executed atleast once. The loop will repeat as long as the specified condition is true. Syntax: do { // Enter here the code to be executed } while (var<=endvalue); E.g. Using do…while loop to display numbers Output: Number:0 Number:1 Number:2 Number:3 Number:4 Note: This loop will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested. 4. JavaScript Break and Continue JavaScript has two special statements that can be used inside loops: a. break: The break command will break the loop and continue executing the code that follows after the loop (if any). E.g Using break statement to come out of the for loop Output: The number is 0 The number is 1 b. Continue: The continue command is used in case you need to break the current loop and continue with the nex

JAVASCRIPT BASICS continued

JavaScript Loops Very often you may want the same block of code to run over and over again. To perform such tasks most of the programming language has loop constructs. Loops in JavaScript execute the same block of code a specified number of times or while a specified condition is true. In JavaScript there are two different kinds of loops: 1. For Loop: For loop is used to loops through a block of code for a specified number of times. The for loop is used when you know in advance how many times the script should run. Syntax for (var=startvalue;var<=endvalue;var=var+increment) { // code to be executed } E.g. The example below defines a loop that starts with i=1. It will continue until i is less than, or equal to 5. i will increase by 1 each time the loop runs. Note: The increment parameter could also be negative, and the <= could be any comparing statement. Output: Number:1 Number:2 Number:3 Number:4 Number:5 2. While Loop: While loop is used when you want

JAVASCRIPT PROGRAMMING CONSTRUCTS

Most of the programming languages have a common set of programming constructs. JavaScript also provides a complete range of basic programming constructs. JavaScript Conditional Statements As in any other programming language conditional statements in JavaScript are used to perform different actions based on different conditions. While writing a code you may want to perform different actions for different decisions. For this JavaScript has following conditional statements: 1. If statement - use this statement if you want to execute some code only if a specified condition is true. Syntax if (condition) { /* code to be executed if condition is true*/ } E.g. Using if statement E.g. Note: in case If is written in lowercase letters using uppercase letters (IF) will generate a JavaScript error! 2. if...else statement:- This statement is used if you want to execute some code if the condition is true and any another code if the condition is false. Syntax: if (cond

Conditional Operator

Conditional Operator JavaScript supports conditional expression operator. They are? and :. These operators are ternary operators as they require three operands , a condition to be evaluated and the alternatives that need to be returned. The return value is based on the condition as it can be true or false. Syntax: variablename=(condition)?value1:value2 E.g. result=(percentage==50)?"Pass":"Fail"; In the above example if the variable percentage has the value 50, then the variable result will be assigned the value “Pass” else it will be assigned "False". 6. Using + Operator with Strings The + operator can be used to add string variables or text values together. In case operands are number + operator performs addition. In case operands are strings it is used to concatenate them. E.g. Concatenating two Strings t1="Welcome"; t2="India"; t3=t1+t2; After the execution of the statements above, the variable t3 will

How to write Javascript?

Writing Comments in javaScript JavaScript comments can be used to make the code more readable. JavaScript Comments can be added to explain the JavaScript, or to make it more readable. You can use single line comment as well as multiple line comments: JavaScript Single Line Comment: Single line comment start with //. Check out the single lime comment written in following example. JavaScript Multi-Line Comments: Multi line comments start with /* and end with */. This example uses a multi line comment to explain the code: JavaScript Operators and Expressions As any other programming language JavaScript has a set of operators that can be used to manipulate data. An operator transforms one or more value into resultant value. The value to which operator is applied is called operands. Operators and its operands are combined to form expression. Expressions can be evaluated to determine value of expression. The set of operators have been discussed below: 1. Arithmetic O

JAVASCRIPT BASICS

JavaScript has programming capabilities similar to the most programming languages such as variables, data types, constants, programming constructs, user defined functions etc.. Characteristics of JavaScript 1. JavaScript is Case Sensitive: Unlike HTML, JavaScript is case sensitive. You need to take care when you write javaScript statements, create or call variables, objects and functions. 2. JavaScript Statements: JavaScript is a sequence of statements that are to be executed by the browser. It is a command to the browser which tells browser what to needs to do. The following JavaScript statement tell the browser to write "Hello" on the web page: document.write("Hello"); 3. You need to add a semicolon at the end of each executable statement. According to the JavaScript standard semicolon is optional and the browser is supposed to interpret the end of the line as the end of the statement. Using semicolons makes it possible to write multiple stateme

Browser support to JavaScript

Browser support to JavaScript It is possible that a browser do not support JavaScript (generally older versions of the browsers). In such cases JavaScript will be displayed as the content of the webpage as explained above. To prevent them from doing this and as a part of the JavaScript standard you should use the HTML comment tag to "hide" the JavaScript. For this you need to add an HTML comment tag (end of comment) after the last JavaScript statement. E.g. Note: In the above example the two forward slashes at the end of comment line (//) are the JavaScript comment symbol. This prevents JavaScript from executing the --> tag. In case browser does not support JavaScript, the JavaScript code will not be displayed in the webpage as it is written inside the HTML comment tag. Using JavaScript and HTML together As you have seen that you can easily embedded JavaScript in a HTML document. Whenever you need to write JavaScript code you need to use In the

UNDERSTANDING JAVA SCRIPT

Advantage of Using JavaScript 1. Embedded with in HTML: JavaScript is embedded with in a HTML. It does not require any separate editor for programs to be written /edited/compiled. Like HTML JavaScript can be written in simple editor such as HTML. 2. Improved Performance: JavaScript is written with the HTML tags and is interpreted by the browser. This minimizes storage requirement on the web server and download time for the client. 3. Minimal Syntax: The rules for the syntax are simple and applications can be quickly built. 4. Designed for Simple Programs: Simple small programs can be easily written and implemented using JavaScript. Such programs can be easily integrated in a webpage. 5. An Interpreted Language: JavaScript is an interpreted language. It does not require a compilation steps. The syntax is interpreted by the browser just as it interprets HTML tags. 6. Good Programming Constructs: Lik

INTRODUCTION TO JAVA SCRIPT

With the popularity of web there is a need to allow users to interact with the website. Websites need to be intelligent enough to accept user’s request/feedback and dynamically change the content of the webpage on the basis of user input. Some changes like changing background when the content get changed or showing content on the basis of input entered by the user helps to attract the users. JavaScript has been developed to improve the design, validate form entries, detect browsers and manipulate their object, create cookies, and much more. It has emerged as one of the popular scripting language on the internet. JavaScript can be understood as a scripting language that adds interactivity to HTML pages. It is embedded directly into HTML pages and is an interpreted language i.e. you can execute a JavaScript without preliminary compilation. In this blog you will learn about using JavaScript with HTML. After completing this tutorial you will be able to: 1. Write simple pr

JAVASCRIPT Problems

Hello Students check these javascript problems Add new elemts at the beginning of array <html > <body > <script language="javascript" > var scripts=new Array(); scripts[0]="PHP"; scripts[1]="ASP"; scripts[2]="JAVASCRIPT"; scripts[3]="HTML"; document.write(scripts.join("< br > ")); document.write("< br >---- NOW AFTER APPLYING unshift()---< br >"); scripts.unshift("VBSCRIPT","PERL"); document.write(scripts.join("< br >")); </script > </body > </html > Display Today's date and Time < html > < title > < /title > < body > < script language="javascript" > var d=new Date; document.write("< b > "+d); < /script > < /body > < /html > Add an element to position 2 in an array < html > < body > < script type=