1.
Boolean :
<!DOCTYPE html>
<html>
<body>
<p>Never create booleans as objects.</p>
<p>Booleans and objects cannot be safely compared.</p>
<p id="demo"></p>
<script>
var x = false; // x is a boolean
var y = new Boolean(false); // y is an object
[Link]("demo").innerHTML =
typeof x + "<br>" + typeof y;
</script>
</body>
</html>
[Link] Arrays
Creating an Array
Using an array literal is the easiest way to create a JavaScript Array.
Syntax:
var array-name = [item1, item2, ...];
Using the JavaScript Keyword new
The following example also creates an Array, and assigns values to it:
Example
var cars = new Array("Saab", "Volvo", "BMW");
Access the Elements of an Array
You refer to an array element by referring to the index number.
This statement accesses the value of the first element in cars:
var name = cars[0];
This statement modifies the first element in cars:
cars[0] = "Opel";
Example
var cars = ["Saab", "Volvo", "BMW"];
[Link]("demo").innerHTML = cars[0];
Access the Full Array
With JavaScript, the full array can be accessed by referring to the array
name:
Example
var cars = ["Saab", "Volvo", "BMW"];
[Link]("demo").innerHTML = cars;
Looping Array Elements
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<p>The best way to loop through an array is using a standard for loop:</p>
<p id="demo"></p>
<script>
var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = [Link];
text = "<ul>";
for (i = 0; i < fLen; i++) {
text += "<li>" + fruits[i] + "</li>";
text += "</ul>";
[Link]("demo").innerHTML = text;
</script>
</body>
</html>
The Difference Between Arrays and
Objects
In JavaScript, arrays use numbered indexes.
In JavaScript, objects use named indexes.
Array methods:
Converting Arrays to Strings
The JavaScript method toString() converts an array to a string of (comma
separated) array values.
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
[Link]("demo").innerHTML = [Link]();
Result
Banana,Orange,Apple,Mango
The join() method also joins all array elements into a string.
It behaves just like toString(), but in addition you can specify the separator:
Example
var fruits = ["Banana", "Orange","Apple", "Mango"];
[Link]("demo").innerHTML = [Link](" * ");
Result
Banana * Orange * Apple * Mango
Popping
The pop() method removes the last element from an array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
[Link](); // Removes the last element ("Mango")
from fruits
Pushing
The push() method adds a new element to an array (at the end):
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
[Link]("Kiwi"); // Adds a new element ("Kiwi") to fruits
The sort() method is one of the strongest array methods.
Sorting an Array
The sort() method sorts an array alphabetically:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
[Link](); // Sorts the elements of fruits
Reversing an Array
The reverse() method reverses the elements in an array.
You can use it to sort an array in descending order:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
[Link](); // Sorts the elements of fruits
[Link](); // Reverses the order of the elements
[Link] object:
<!DOCTYPE html>
<html>
<body>
<p>The internal clock in JavaScript starts at midnight January 1, 1970.</p>
<p>The getTime() function returns the number of milliseconds since then:</p>
<p id="demo"></p>
<script>
var d = new Date();
[Link]("demo").innerHTML = [Link]();
</script>
<p id="demo1"></p>
<script>
var d = new Date();
[Link]("demo1").innerHTML = [Link]();
</script>
<p id="demo2"></p>
<script>
var d = new Date();
[Link]("demo2").innerHTML = [Link]();
</script>
<p id="demo3"></p>
<script>
var d = new Date();
[Link]([Link]() + 50);
[Link]("demo3").innerHTML = d;
</script>
</body>
</html>
[Link] object:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Math object constants and methods</h1>
<p id="demo"></p>
<script>
[Link]("demo").innerHTML =
"<p><b>[Link]:</b> " + [Link] + "</p>" +
"<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
"<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
"<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" ;
</script>
<p id="demo1"></p>
<script>
[Link]("demo1").innerHTML = "<p><b>Math round:</b> " + [Link](4.4)
+ "</p>" +
"<p><b>Math pow:</b> " + [Link](8, 2) + "</p>" +
"<p><b>Math sqrt:</b> " + [Link](64)+ "</p>" +
"<p><b>Math ceil:</b> " + [Link](4.4)+ "</p>" +
"<p><b>Math floor:</b> " + [Link](4.7)+ "</p>" ;
</script>
</body>
</html>
5. JavaScript Numbers
JavaScript has only one type of number.
Numbers can be written with, or without, decimals
The toString() Method
toString() returns a number as a string.
All number methods can be used on any type of numbers (literals, variables,
or expressions):
Example
var x = 123;
[Link](); // returns 123 from variable x
The toExponential() Method
toExponential() returns a string, with a number rounded and written using
exponential notation.
A parameter defines the number of characters behind the decimal point:
Example
var x = 9.656;
[Link](2); // returns 9.66e+0
The toFixed() Method
toFixed() returns a string, with the number written with a specified number
of decimals:
Example
var x = 9.656;
[Link](0); // returns 10
[Link](2); // returns 9.66
[Link](4); // returns 9.6560
Example:
<!DOCTYPE html>
<html>
<body>
<p>The toExponential() method returns a string, with the number rounded and written using
exponential notation.</p>
<p>An optional parameter defines the number of digits behind the decimal point.</p>
<p id="demo"></p>
<script>
var x = 9.656;
[Link]("demo").innerHTML =
[Link]() + "<br>" +
[Link](2) + "<br>" +
[Link](4) + "<br>" +
[Link](6);
</script>
</body>
</html>
[Link] Objects
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:
var car = {type:"Fiat", model:"500", color:"white"};
The values are written as name:value pairs (name and value separated by a
colon).
JavaScript objects are containers for named values.
Object Properties
The name:values pairs (in JavaScript objects) are called properties.
var person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};
Object Methods
Methods are actions that can be performed on objects.
Accessing Object Properties
You can access object properties in two ways:
[Link]
or
objectName["propertyName"]
Example1
[Link];
Example2
person["lastName"];
Accessing Object Methods
You access an object method with the following syntax:
[Link]()
Example
name = [Link]();
Example:
<html>
<body>
<p>Creating and using an object method.</p>
<p>An object method is a function definition, stored as a property value.</p>
<p id="demo"></p>
<script>
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return [Link] + " " + [Link];
};
[Link]("demo").innerHTML = [Link]();
</script>
</body>
</html>
[Link] Strings
A JavaScript string simply stores a series of characters like "John Doe".
A string can be any text inside quotes. You can use single or double quotes:
Example
var carname = "Volvo XC60";
var carname = 'Volvo XC60';
String Length
The length property returns the length of a string:
Example
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = [Link];
Finding a String in a String
The indexOf() method returns the index of (the position of) the first
occurrence of a specified text in a string:
Example
var str = "Please locate where 'locate' occurs!";
var pos = [Link]("locate");
The lastIndexOf() method returns the index of the last occurrence of a
specified text in a string:
Example
var str = "Please locate where 'locate' occurs!";
var pos = [Link]("locate");
Replacing String Content
The replace() method replaces a specified value with another value in a
string:
Example
str = "Please visit Microsoft!";
var n = [Link]("Microsoft", "W3Schools");
Converting to Upper and Lower Case
A string is converted to upper case with toUpperCase():
Example
var text1 = "Hello World!"; // String
var text2 = [Link](); // text2 is text1 converted to
upper
A string is converted to lower case with toLowerCase():
Example
var text1 = "Hello World!"; // String
var text2 = [Link](); // text2 is text1 converted to
lower
Example:
[Link]
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
[Link]("demo").innerHTML = [Link];
</script>
<p id="p1">Please locate where 'locate' occurs!.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = [Link]("p1").innerHTML;
var pos = [Link]("locate");
[Link]("demo").innerHTML = pos;
</script>
</body>
</html>
8. JavaScript Regular Expressions
What Is a Regular Expression?
A regular expression is a sequence of characters that forms a search
pattern.
When you search for data in a text, you can use this search pattern to
describe what you are searching for.
A regular expression can be a single character, or a more complicated
pattern.
Regular expressions can be used to perform all types of text search and
text replace operations.
Syntax
/pattern/modifiers;
Example
var patt = /w3schools/i;
Example explained:
/w3schools/i is a regular expression.
w3schools is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-insensitive).
Example:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to do a global search for the characters "i" and
"s" in a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "Do you know if this is all there is?";
var patt1 = /[is]/gi;
var result = [Link](patt1);
[Link]("demo").innerHTML = result;
</script>
</body>
</html>