Professional Documents
Culture Documents
Javascript: Variable: Let (New) : Let Abc Abc "Hello" - Let Abc "Hello" Var (Old) : Var Abc "Hello"
Javascript: Variable: Let (New) : Let Abc Abc "Hello" - Let Abc "Hello" Var (Old) : Var Abc "Hello"
JavaScript can capture various user actions on a web page in the form of different Javascript
events. JavaScript can change the existing HTML of a web page after it is being loaded by
the browser. JavaScript can be used to create highly interactive web pages.
The web browser makes a separate HTTP request for the JavaScript file. The web server
simply sends the file back to the browser as response, without even bothering to know what
it contains. Once the response comes back, the browser executes the JavaScript file.
JavaScript is a client side scripting language that supports object orientation. The file
extension of the JavaScript files should be .js. <script> tag is used to add JavaScript to a
web page.
Variable:
Let (new): let abc;
abc = “hello”; || let abc = “hello”;
Var(old) : var abc = “hello”;
JavaScript
Operators:
Extra : arithmetic operator : exponential operator ( ** )
Assignment operator : exponential equal to ( **= )
Typeof : it tells us the operand type.
JavaScript
In JavaScript, you can initialise a variable using let, const, or var. We do not need to specify
the datatype while defining a variable.
Variables are containers that are used to store different types of data or information.
Variables are declared using the let or the var keyword. If no keyword is used while declaring
variables, the variable becomes a global variable.
variable name can contain alpha-numeric characters, the underscore, and the dollar but not
any other special characters. A variable name cannot start with a number. It must start with
either an alphabet, the underscore or the dollar. Variable names are case sensitive.
Meaning, abc and Abc are two different variables.
Conditional statement :
1. If
2. Else
3. If-else if-else
4. Switch
switch(variable){
case “1”:
statement;
break;
case”2”:
statement;
break;
.
.
.
Default:
Statement;
Break;}
Loops:
1. while
while(condition){}
2. do-while
do{
JavaScript
}while(condition);
3. for
for(initialization ; condition ; iteration){
statement;
}
4. for-of loop :
for(let element of array){statement}
5. for-in loop :
for(let name on object){statement} // key value pair
6. foreach
The while loop executes a block of code if and as long as a specific condition is
true.
The do-while loop executes the code block inside the loop at least once, even if the condition
fails for the very first time.
The for-of loop is used to iterate through arrays. The loop runs as many times as the number
of elements in the array. In each iteration it picks up one element of the array and assigns it
to a variable for us to use.
The for-in loop works exclusively on JavaScripts objects. The loop would iterate through all
the key-value pairs in an object. In each iteration it picks up one key and assigns it to a
variable for us to use.
JavaScript
The forEach is not a loop, it is a function. The forEach function is used to iterate through
arrays, not objects. The forEach is not a loop, it is a function. The forEach function is used to
iterate through arrays, not objects.
Function :
Function call:
JavaScript
Output: 42,4,6,8,108,4,6,8,10
JavaScript
A function is a block of code which may or may not have a name. A function has two parts:
a function definition and a function call. A function is created when the same piece of code
might be used at multiple places. A function can return only one value.
Anonymous functions are stored in variables. An anonymous function can be called /
invoked using the variable name. The syntax for definition for an anonymous function ends
with a semicolon. The function keyword is not used in the arrow function notation.
let dog = {
breed: "gold",
height: "4ft",
age : 2,
JavaScript
display: function(){
document.write(this.breed+this.height+this.age);
}
};
dog.weight = 223;
document.write(dog.breed);
document.write(dog.height);
document.write(dog.age);
document.write(dog.weight);
document.write(dog['breed']);
document.write(dog['height']);
document.write(dog['age']);
document.write(dog['weight']);
dog.display();
dog.breed= "gold";
dog.height= "height";
dog.age= 2;
dog.display = function() { document.write(this.breed+this.height+this.age);
};
document.write(dog.breed);
document.write(dog.height);
document.write(dog.age);
dog.display();
Function constructor: it is a normal function used to create an object.
function car(make,model,year){
this.make = make;
this.model = model;
this.year = year;
this.display = function() {
document.write(this.make+this.model+this.year);
};
}
car1.display();
car2.display();
car3.display();
JavaScript
object using class:
class car{
constructor(make,model,year){
this.make = make;
this.model = model;
this.year = year;
}
display () {
document.write(this.make+this.model+this.year);
};
};
let car1 = new car("hyndai","i2o",2014);
let car2 = new car("honda","city",2005);
let car3 = new car("tata","nano",2012);
Arrays:
let mod = ["html","css","bootstrap","dbms"];
for(let i of mod){
document.write(i+" ");
}
mod[2] = "css";
mod[4] = "php";
mod.push("php");
for(let i of mod){
document.write(i+" ");
}
arr.forEach(function(element){
document.write(element+" ");
});
squares.forEach(function(element){
document.write(element+" ");
});
let s = sum(1,3,5,7);
document.write(s+" ");
s = sum(2,4,6,8);
document.write(s+" ");
let s = sum(1,3,5,7);
document.write(s+" ");
s = sum(2,4,6,8);
document.write(s+" ");
Strings : A string is any text or a sequence of characters. They are enclosed within quotes
(single quotes or double quotes). They can be either primitive strings or string objects.
string as object:
let str = new String("hello");
document.write(str.length);
indexOf()
JavaScript
The indexOf() method returns the index of (the position of)
the first occurrence of a specified text in a string:
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
output : 7
slice()
output: banana
replace()
startsWith()
text.startsWith("Hello");
JavaScript
output:true
endsWith()
include()
Method:
getElementById
The getElementById() method returns only the first HTML element that
matches the given id.
Properties:
innerHTML : it helps to fetch the content of the element.
The innerHTML property is used to both fetch and set the content of an HTML
element.
let tag = document.getElementById("abc");
alert(tag.innerHTML);
tag.innerHTML = "this is new para";
tag.style.color = "red";
Event handler: OnClick : it is used to capture user clicks.
Event handlers go by the name onEventName. The event handlers
continue to run forever, till the web page is closed. There is an
alternative way of using event handlers - as attributes to HTML
elements.
let tag = document.getElementById("abc");
tag.onclick = function(){
tag.innerHTML = "this is new para";
tag.style.color = "red";
}
Since, JavaScript runs on the browser, JavaScript can capture various user actions on a
web page in the form of different Javascript events, and also, it can make changes to the
existing HTML elements of a web page even after they have been rendered by the browser.
The replace() function replaces only the first occurrence of the first parameter with the
second one.
slice(-30) indicates extract last 30 elements from the sequence.
The onclick event executes the defined function when the user clicks the specific element on
which the onclick event handler is set.
JavaScript
No, the browser can never know what all is present on the server, unless the server sends
that information to the browser. The sources tab displays only those files that get loaded on
the browser while fetching the main web page.
Step-over takes the code execution to the very next line, but in the paused state.
The httpRequest.status stores the value of the HTTP response code. When an Ajax request
is successfully completed, the response code is 200.