What is JavaScript?
- JavaScript is a Scripting Language.
- JavaScript is programming code that can be inserted into HTML pages.
- All modern HTML pages are using JavaScript.
- JavaScript code can be executed by all modern web browsers.
JavaScript can "display" data in different ways:
- Writing into an HTML element, using innerHTML.
- Writing into the HTML output using document.write().
- Writing into an alert box, using window.alert().
- Writing into the browser console, using console.log().
<html>
<body>
<h1>My First Example</h1>
<p>My First Style</p>
<script>
document.getElementById("demo").innerHtml= 5+6; //innerHTML
document.write(5+6); //document.write
window.alert(5+6); //window.alert
console.log(5+6); //console.log
</script>
</body>
</html>
JavaScript Keywords
Keywords Description
- break Terminates a switch or a loop
- continue Jumps out of a loop and starts at the top
- debugger Stops the execution of JavaScript, and calls the debugging function
- do...while Executes a block of statements, and repeats the block, while a condition is true
- for Marks a block of statements to be executed, as long a condition is true
- function Declares a function
- if...else Marks a block of statement to be executed, depending on a condition
- return Exits a function
- switch Marks a block of statement to be executed, depending on different cases
- try...catch Implements error handling to a block of statements
- var Declares a variable
JavaScript Comments
- Single line comments start with //
- Multi-line comments start with /* and end with */
External JavaScript Advantages
- It Separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
JavaScript Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus
++ Increment
-- Decrement
& AND
~ NOT
^ XOR
<< Zero fill left shift
>> Signed right shift
>>> Zero fill right shift
Creating an Array
Using an Array literal is the easiest way to create a JavaScript Array.
Syntax :
Type :- 01
var array_name=[item1,item2,....]
Type :- 02
var array_name=new Array(item1,item2,....)
Array Elements
Using an Array literal is the easiest way to create a JavaScript Array.
Syntax :
Type :- 01
var array_name=[item1,item2,....]
Type :- 02
var array_name=new Array(item1,item2,....)
Array Elements
- Popping - pop() - This method removes the last element from an array.
- Pushing - push() - This method adds a new element to an array(at the end).
- Shifting - shift() - This method removes the first array element and "shifts" all other elements to alower index.
- Unshifting - unshift() - This method adds a new element to an array (at the beginning), and "unshifts" older elements.
- Deleting - delete - Since JavaScript arrays are objects, elements can be deleted by using the JavaScript operator delete.
- Splicing - splice() - This method can be used to add new items to an array.
- Concatenating - concat() - This method creates to a new array by merging (concatenating) existing arrays.
- Slicing - slice() - This method slices out a piece of an array into a new array.
- Sorting - sort() - This method sorts an array alphabetically.
- Reversing - reverse() - This method reverses the elements in an array.
- Maximum - math.max.apply() - You can use math.max.apply to find the highest number in an array.
- Minimum - math.min.apply() - You can use math.min.apply to find the lowest number in an array.
- for - loops through a block of code a number of times
- for/in - loops through the properties of an object
- for/of - loops through the values of an iterable object
- while - loops through a block of code while a specified condition is true
- do/while - also loops through a block of code while a specified condition is true
No comments:
Post a Comment