Skip to main content

Posts

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