html
html
Output:
This is a Heading
This is a paragraph.
Example Explained
An HTML element is defined by a start tag, some content, and an end tag:
<tag name> Content goes here... </tag name>
The HTML element is everything from the start tag to the end tag:
Note: Some HTML elements have no content (like the <br> element). These
elements are called empty elements. Empty elements do not have an end
tag!
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Note: The content inside the <body> section will be displayed in a browser.
The content inside the <title> element will be shown in the browser's title
bar or in the page's tab.
Screen reader and other accessible tools use this outline to obtain the
structure of a web page and provide this information to the visually impaired
users to navigate the web page easily.
Search engines use outline of a page to understand its structure better.
Properly outlined webpage is easy to navigate from one section to other
using keyboard.
Using HTML document outline allows you to use more than six levels of
headings, actually as many as you want.
Rules of outlining an HTML document?
When outlining a webpage using, remember the following points (it includes
HTML outline algorithm’s rules)-
Heading elements (h1 to h6) and sectioning elements (section, article, nav,
and aside) are used to outline a webpage.
Each sectioning element demarcates a separate section.
Separated sectioning elements create same level in the document outline no
matter which heading elements are used inside the sectioning elements. See
the (a) diagram in the following image-
Nested section creates a sub level under its parent. See the (b) diagram in
the above image.
Each sectioning element has its own scope of heading (h1 – h6). That means
a webpage may have more than one h1 or other heading elements.
Body element is the root section which comprises all the remaining sections.
The first heading which is outside of any sectioning element is the heading
of the body root section.
Any sectioning element inside the body element or the other sectioning
elements is the nested section of the body or other sectioning elements
respectively.
If there is more than one heading in a section, the first one is the heading of
that section.
How to view an outline of a web page
You can use the following online based HTML5 outliner tool to view outline of
your web page. Simply paste the url of the page or paste the HTML source
code in the box- http://gsnedders.html5.org/outliner/
Example of outlining an HTML document
At first look at the following HTML which is valid codes for both HTML5 and
the previous versions.
1 <body>
4 <h3>HTML</h3>
5 <h3>CSS</h3>
6 <h3>JavaScript</h3>
7 <h3>jQuery</h3>
11 <h3>Database: MySQL</h3>
12 </body>
HTML Head
The HTML <head> element is used as a container for metadata (data about
data). It is used between <html> tag and <body> tag.
An HTML head can contain lots of metadata information or can have very
less or no information, it depends on our requirement. But head part has a
crucial role an HTML document while creating a website.
Metadata defines the document title, character set, styles, links, scripts, and
other meta information.
o <title>
o <style>
o <meta>
o <link>
o <script>
o <base>
The HTML <title> element is used to define the title of the document. It is
used in all HTML/XHTML documents. The <title> element must be placed
between <head> element, and one document can only have one title
element.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>This Page Title</title>
5. </head>
6. <body>
7. <p>The body's content is displayed in the browser window.</p>
8. <p>The content of the title element is displayed in the browser tab, in favori
tes and in search engine results.</p>
9. </body>
10. </html>
HTML <style> Element
The HTML <style> element is used to style the HTML page. The <style>
element can have CSS properties for that HTML page only. If we want to
apply CSS for multiple pages then we should use separate CSS file.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>This is Page Title</title>
5. <style>
6. body {background-color: pink;}
7. h1 {color: red;}
8. p {color: blue;}
9. </style>
10. </head>
11. <body>
12. <h1>This is a Heading</h1>
13. <p>This is a paragraph.</p>
14. </body>
15. </html>
HTML <link> Element
The HTML <link> element is used to link an external style sheet to your
webpage. The <link> element contains main two attributes which are "rel"
and "href". The rel attribute indicates that it is a stylesheet, and href gives
the path to that external file.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>This is title</title>
5. <link rel="stylesheet" href="style.css">
6. </head>
7. <body>
8. <h2>Web-page with external CSS</h2>
9. <p>This is looking a cool page</p>
10. </body>
11. </html>
HTML <meta> Element
The HTML <meta> element is used to specify the character set, page
description, keywords, authors and other metadata on the webpage.
Metadata is mainly used by browsers, search engines, and other web
services to rank your webpage better.
<meta charset="UTF-8">
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset="UTF-8">
5. </head>
6. <body>
7. <p>This is written in English language<span style="color: blue"> My friend'
s name is.......</span></p>
8. <p>This is Chinese language <span style="color: red">Wǒ de péngyǒu jiào
</span></p>
9. </body>
10. </html>
The <body> must be the second element after the <head> tag or it should
be placed between </head> and </html> tags. This tag is required for every
HTML document and should only use once in the whole HTML document.
Syntax
Usage Structural
Example
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Body Tag</title>
5. </head>
6. <body>
7. <h2>Example of body tag</h2>
8. <p>This paragraph is written between the body tag</p>
9. </body>
10. </html>
Output:
Global Attribute
Event Attribute
HTML Form
HTML forms are required if you want to collect some data from of the site
visitor.
For example: If a user want to purchase some items on internet, he/she must
fill the form such as shipping address and credit/debit card details so that
item can be sent to the given address.
Tag Description
Tag Description
The HTML <form> element provide a document section to take input from
user. It provides various interactive controls for submitting information to
web server such as text field, text area, password field, etc.
Note: The <form> element does not itself create a form but it is container to
contain all required form elements, such as <input>, <label>, etc.
Syntax:
1. <form>
2. //Form elements
3. </form>
Example:
1. <body>
2. <form>
3. Enter your name <br>
4. <input type="text" name="username">
5. </form>
6. </body>
Output:
ADVERTISEMENT
The type="text" attribute of input tag creates textfield control also known as
single line textfield control. The name attribute is optional, but it is required
for the server side component such as JSP, ASP, PHP etc.
1. <form>
2. First Name: <input type="text" name="firstname"/> <br/>
3. Last Name: <input type="text" name="lastname"/> <br/>
4. </form>
Output:
Note: If you will omit 'name' attribute then the text filed input will not be
submitted to server.
HTML <textarea> tag in form
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form in HTML</title>
5. </head>
6. <body>
7. <form>
8. Enter your address:<br>
9. <textarea rows="2" cols="20"></textarea>
10. </form>
11. </body>
12. </html>
Output:
If you click on the label tag, it will focus on the text control. To do so, you
need to have for attribute in label tag that must be same as id attribute of
input tag.
NOTE: It is good to use <label> tag with form, although it is optional but if
you will use it, then it will provide a focus when you tap or click on label tag.
It is more worthy with touchscreens.
1. <form>
2. <label for="firstname">First Name: </label> <br/>
3. <input type="text" id="firstname" name="firstname"/> <br/>
4. <label for="lastname">Last Name: </label>
5. <input type="text" id="lastname" name="lastname"/> <br/>
6. </form>
Output:
1. <form>
2. <label for="password">Password: </label>
3. <input type="password" id="password" name="password"/>
<br/>
4. </form>
Output:
HTML 5 Email Field Control
The email field in new in HTML 5. It validates the text for correct email
address. You must use @ and . in this field.
1. <form>
2. <label for="email">Email: </label>
3. <input type="email" id="email" name="email"/> <br/>
4. </form>
Note: If we will not enter the correct email, it will display error like:
The radio button is used to select one option from multiple options. It is used
for selection of gender, quiz questions etc.
If you use one name for all the radio buttons, only one radio button can be
selected at a time.
Using radio buttons for multiple options, you can only choose a single option
at a time.
1. <form>
2. <label for="gender">Gender: </label>
3. <input type="radio" id="gender" name="gender" value="male"/
>Male
4. <input type="radio" id="gender" name="gender" value="female"/
>Female <br/>
5. </form>
Checkbox Control
1. <form>
2. Hobby:<br>
3. <input type="checkbox" id="cricket" name="cricket" value="crick
et"/>
4. <label for="cricket">Cricket</label> <br>
5. <input type="checkbox" id="football" name="football" value="foo
tball"/>
6. <label for="football">Football</label> <br>
7. <input type="checkbox" id="hockey" name="hockey" value="hoc
key"/>
8. <label for="hockey">Hockey</label>
9. </form>
Note: These are similar to radio button except it can choose multiple options
at a time and radio button can select one button at a time, and its display.
Output:
Submit button control
Syntax:
The value attribute can be anything which we write on button on web page.
Example:
1. <form>
2. <label for="name">Enter name</label><br>
3. <input type="text" id="name" name="name"><br>
4. <label for="pass">Enter Password</label><br>
5. <input type="Password" id="pass" name="pass"><br>
6. <input type="submit" value="submit">
7. </form>
Output:
HTML <fieldset> element:
Example:
1. <form>
2. <fieldset>
3. <legend>User Information:</legend>
4. <label for="name">Enter name</label><br>
5. <input type="text" id="name" name="name"><br>
6. <label for="pass">Enter Password</label><br>
7. <input type="Password" id="pass" name="pass"><br>
8. <input type="submit" value="submit">
9. </fieldset>
10. </form>
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form in HTML</title>
5. </head>
6. <body>
7. <h2>Registration form</h2>
8. <form>
9. <fieldset>
10. <legend>User personal information</legend>
11. <label>Enter your full name</label><br>
12. <input type="text" name="name"><br>
13. <label>Enter your email</label><br>
14. <input type="email" name="email"><br>
15. <label>Enter your password</label><br>
16. <input type="password" name="pass"><br>
17. <label>confirm your password</label><br>
18. <input type="password" name="pass"><br>
19. <br><label>Enter your gender</label><br>
20. <input type="radio" id="gender" name="gender" value="male"/
>Male <br>
21. <input type="radio" id="gender" name="gender" value="female
"/>Female <br/>
22. <input type="radio" id="gender" name="gender" value="others
"/>others <br/>
23. <br>Enter your Address:<br>
24. <textarea></textarea><br>
25. <input type="submit" value="sign-up">
26. </fieldset>
27. </form>
28. </body>
29. </html>
Output:
1. <form action="#">
2. <table>
3. <tr>
4. <td class="tdLabel"><label for="register_name" class="label">Enter na
me:</label></td>
5. <td><input type="text" name="name" value="" id="register_name" sty
le="width:160px"/></td>
6. </tr>
7. <tr>
8. <td class="tdLabel"><label for="register_password" class="label">Ente
r password:</label></td>
9. <td><input type="password" name="password" id="register_password"
style="width:160px"/></td>
10. </tr>
11. <tr>
12. <td class="tdLabel"><label for="register_email" class="label">Ent
er Email:</label></td>
13. <td
14. ><input type="email" name="email" value="" id="register_email" sty
le="width:160px"/></td>
15. </tr>
16. <tr>
17. <td class="tdLabel"><label for="register_gender" class="label">E
nter Gender:</label></td>
18. <td>
19. <input type="radio" name="gender" id="register_gendermale" value
="male"/>
20. <label for="register_gendermale">male</label>
21. <input type="radio" name="gender" id="register_genderfemale" valu
e="female"/>
22. <label for="register_genderfemale">female</label>
23. </td>
24. </tr>
25. <tr>
26. <td class="tdLabel"><label for="register_country" class="label">S
elect Country:</label></td>
27. <td><select name="country" id="register_country" style="width:1
60px">
28. <option value="india">india</option>
29. <option value="pakistan">pakistan</option>
30. <option value="africa">africa</option>
31. <option value="china">china</option>
32. <option value="other">other</option>
33. </select>
34. </td>
35. </tr>
36. <tr>
37. <td colspan="2"><div align="right"><input type="submit" id="re
gister_0" value="register"/>
38. </div></td>
39. </tr>
40. </table>
41. </form>
Chapter 2
What is JavaScript
JavaScript (js) is a light-weight object-oriented programming language which
is used by several websites for scripting the webpages. It is an interpreted,
full-fledged programming language that enables dynamic interactivity on
websites when applied to an HTML document.
It was introduced in the year 1995 for adding programs to the webpages in
the Netscape Navigator browser. Since then, it has been adopted by all other
graphical web browsers. With JavaScript, users can build modern web
applications to interact directly without reloading the page every time. The
traditional website uses js to provide several forms of interactivity and
simplicity.
Features of JavaScript
There are following features of JavaScript:
History of JavaScript
In 1993, Mosaic, the first popular web browser, came into existence. In
the year 1994, Netscape was founded by Marc Andreessen. He realized
that the web needed to become more dynamic. Thus, a 'glue language' was
believed to be provided to HTML to make web designing easy for designers
and part-time programmers. Consequently, in 1995, the company
recruited Brendan Eich intending to implement and embed Scheme
programming language to the browser. But, before Brendan could start, the
company merged with Sun Microsystems for adding Java into its Navigator
so that it could compete with Microsoft over the web technologies and
platforms. Now, two languages were there: Java and the scripting language.
Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog box,
confirm dialog box and prompt dialog box),
o Displaying clocks etc.
JavaScript Example
1. <script>
2. document.write("Hello JavaScript by JavaScript");
3. </script>
Statements (JavaScript)
import (JavaScript)
Imports code from a script library.
Operators (JavaScript)
String operators + +=
(JavaScript)
1 . [] le
new rig
2 () le
3 ++ -- no
5 */% le
6 +- le
9 == != === !== le
10 & le
11 ^ le
12 | le
13 && le
14 || le
15 ?: rig
17 , le
_dump (JavaScript)
Prints a string representation of an object to the log file.
eval (JavaScript)
Evaluates an expression or statements.
isNaN (JavaScript)
Determines whether the result of a string parsed by parseInt or parseFloat is
a valid number.
parseFloat (JavaScript)
Parses a string and returns a floating point number.
parseInt (JavaScript)
Parses a string and returns an integer of the specified radix or base.
print (JavaScript)
Prints a string to the log file.
@Functions (JavaScript)
@Abs (JavaScript)
Returns the absolute (unsigned) value of a number.
@Abstract (JavaScript)
Abbreviates the content of fields.
@Adjust (JavaScript)
Adjusts a time and date by years, months, days, hours, minutes, and/or
seconds.
@AttachmentLengths (JavaScript)
Returns the lengths in bytes of the files attached to the current document.
@AttachmentModifiedTimes (JavaScript)
Returns the modification dates and times of the files attached to the current
document.
@AttachmentNames (JavaScript)
Returns the names of the files attached to the current document.
@Attachments (JavaScript)
Returns the number of files attached to the current document.
@Author (JavaScript)
Returns the names of the authors of the current document.
@Begins (JavaScript)
Determines whether a string begins with a substring.
@Char (JavaScript)
Returns a character that corresponds to a Unicode value.
@ClientType (JavaScript)
Returns a text string to differentiate Notes® clients and Web browsers.
@Contains (JavaScript)
Determines whether a string contains a substring.
@Count (JavaScript)
Returns the number of entries in a list.
@Created (JavaScript)
Returns the creation date and time of the current document.
@Date (JavaScript)
Creates a date and time.
@Day (JavaScript)
Returns the day from a date and time.
@DbColumn (JavaScript)
Returns the values of a view column.
@DbLookup (JavaScript)
Returns view column or field values that correspond to matched keys in a
sorted view column.
@DbName (JavaScript)
Returns the server name and the database name of the current database.
@DbTitle (JavaScript)
Returns the label of the current database.
@Do (JavaScript)
Evaluates a series of @function statements.
@Element (JavaScript)
Returns an element of a list.
@Elements (JavaScript)
Returns the number of entries in a list.
@Ends (JavaScript)
Determines whether a string ends with a substring.
@Error (JavaScript)
Assigns an error condition to a value.
@Explode (JavaScript)
Parses a string into elements in a list using specified separators.
@Failure (JavaScript)
Returns a message.
@False (JavaScript)
Returns the number 0.
@GetField (JavaScript)
Returns the value of a document item associated with the current page.
@Hour (JavaScript)
Returns the hour from a date and time.
@If (JavaScript)
Executes a statement based on a conditional test.
@Implode (JavaScript)
Concatenates elements of a text list into a string.
@Integer (JavaScript)
Truncates a number to an integer.
@IsAvailable (JavaScript)
Indicates whether an item exists in the current document.
@IsDocBeingLoaded (JavaScript)
Returns 1 if the current document is being loaded.
@IsDocBeingSaved (JavaScript)
Returns 1 if the current document is being saved.
@IsError (JavaScript)
Indicates if a value is an error condition.
@IsMember (JavaScript)
Indicates if a list contains a string or strings.
@IsNewDoc (JavaScript)
Indicates if the current document is a new document.
@IsNotMember (JavaScript)
Indicates if a list does not contain a string or strings.
@IsNull (JavaScript)
Indicates if a value is an empty string or list of empty strings.
@IsNumber (JavaScript)
Indicates if a value is a number.
@IsResponseDoc (JavaScript)
Indicates if the current document is a response document.
@IsText (JavaScript)
Indicates if a value is a string.
@IsTime (JavaScript)
Indicates if a value is a date and time.
@IsUnavailable (JavaScript)
Indicates whether an item exists in the current document.
@Left (JavaScript)
Returns the leftmost characters of a string for the number of characters
specified, or up to and excluding a substring.
@LeftBack (JavaScript)
Returns the leftmost characters of a string minus the number of characters
specified, or up to and excluding the last occurrence of a substring.
@Length (JavaScript)
Returns the number of characters in a string.
@List (JavaScript)
Creates a list.
@LowerCase (JavaScript)
Converts a string to lowercase.
@Max (JavaScript)
Returns the largest number in a group of numbers.
@Member (JavaScript)
Returns the position of the first occurrence of a value in a string list.
@Middle (JavaScript)
Returns characters of a string, starting at an offset from the beginning or
after a substring, for the number of characters specified or up to and
excluding a substring.
@MiddleBack (JavaScript)
Returns characters of a string, starting at an offset from the end or before
the last occurrence of a substring, back to and excluding the last occurrence
of a substring or for the negative number of characters specified.
@Min (JavaScript)
Returns the smallest number in a group of numbers.
@Minute (JavaScript)
Returns the minute from a date and time.
@Modified (JavaScript)
Returns a time-date value indicating when the document was modified
initially.
@Modulo (JavaScript)
Returns the remainder of a division operation.
@Month (JavaScript)
Returns the month from a date and time.
@Name (JavaScript)
Returns a component of or reformats a hierarchical name.
@NewLine (JavaScript)
Returns the newline character.
@No (JavaScript)
Returns the number 0.
@Now (JavaScript)
Returns the server date and time.
@Null (JavaScript)
Returns an empty string.
@ProperCase (JavaScript)
Converts a string to propercase.
@Random (JavaScript)
Returns a random number greater than or equal to 0.0 and less than 1.0.
@Repeat (JavaScript)
Repeats a string.
@Replace (JavaScript)
Replaces strings in a list.
@ReplaceSubstring (JavaScript)
Replaces substrings in a list.
@ReplicaID (JavaScript)
Returns the replica ID of the current database.
@Return (JavaScript)
Returns execution to the calling function or the user interface.
@Right (JavaScript)
Returns the rightmost characters of a string for the number of characters
specified, or starting after a substring.
@RightBack (JavaScript)
Returns the rightmost characters of a string minus the number of characters
specified, or back to and excluding the last occurrence of a substring.
@Round (JavaScript)
Rounds a number.
@Second (JavaScript)
Returns the second from a date and time.
@Select (JavaScript)
Returns a value from a parameter list.
@SetField (JavaScript)
Sets a document item associated with the current page to a value.
@Subset (JavaScript)
Subsets a list from left to right.
@Success (JavaScript)
Returns 1 (true).
@Sum (JavaScript)
Sums numbers.
@Text (JavaScript)
Converts a value to a string.
@TextToNumber (JavaScript)
Converts a string to a number.
@TextToTime (JavaScript)
Converts a string to a date and time.
@Time (JavaScript)
Creates a date and time.
@Today (JavaScript)
Returns today's date.
@Tomorrow (JavaScript)
Returns tomorrow's date.
@Trim (JavaScript)
Removes leading and trailing spaces from a string.
@True (JavaScript)
Returns the number 1.
@Unique (JavaScript)
Returns a random, unique text value (no parameter), or removes duplicate
values from a list (parameter).
@UpperCase (JavaScript)
Converts a string to uppercase.
@UserId (JavaScript)
Returns the user ID object for a given user in the ID Vault.
@UserName (JavaScript)
Returns the distinguished name of the current user.
@UserSecretKeys (JavaScript)
Returns the secret keys stored in a user's ID file in the ID Vault.
@Weekday (JavaScript)
Returns the day of the week where Sunday is 1.
@Word (JavaScript)
Returns a word from a string.
@Year (JavaScript)
Returns the year from a date and time.
@Yes (JavaScript)
Returns the value 1.
@Yesterday (JavaScript)
Returns yesterday's date.
You can associate a variable with a data type to document your code and
direct content assist.
Java classes
Java™ classes can be accessed from JavaScript™ code by specifying the fully
qualified name, for example, java.lang.Class or java.util.Random. The
package containing the class must be available as a shared library on the
server. The standard Java™ packages are available on all servers; other
packages must be installed.
User classes
User Java™ classes can be created in an application using
the Domino® or Java™ perspective, and called in JavaScript™ by specifying
the fully qualified name. The class must belong to a package (not the default
package).
Java arrays
Java™ arrays can be embedded in the JavaScript™ code without prior
declarations or inclusions. This provides the ability to create multi-dimension
arrays and to pass them as parameters to Java™ methods included in
the JavaScript™ code.
JavaScript objects
A JavaScript™ object (when created with new Object()) is automatically
converted to a map of properties when used as an argument to
a Java™ method. A map is a Java™ interface that provides a list of
name/value pairs. For example, you should pass a map when you call an
XML adapter.
Parameter conversion
JavaScript™ parameters are passed by value (JavaScript™ arrays and objects
used as parameters are not modified by the Java™ code).
JavaScript Objects
A javaScript object is an entity having state and behavior (properties and
method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.
JavaScript is template based not class based. Here, we don't create class to
get the object. But, we direct create objects.
Creating Objects in JavaScript
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
object={property1:value1,property2:value2.....propertyN:valueN}
1. <script>
2. emp={id:102,name:"Shyam Kumar",salary:40000}
3. document.write(emp.id+" "+emp.name+" "+emp.salary);
4. </script>
Output o the above example
102 Shyam Kumar 40000
1. <script>
2. var emp=new Object();
3. emp.id=101;
4. emp.name="Ravi Malik";
5. emp.salary=50000;
6. document.write(emp.id+" "+emp.name+" "+emp.salary);
7. </script>
Output of the above example
101 Ravi 50000
Here, you need to create function with arguments. Each argument value can
be assigned in the current object by using this keyword.
1. <script>
2. function emp(id,name,salary){
3. this.id=id;
4. this.name=name;
5. this.salary=salary;
6. }
7. e=new emp(103,"Vimal Jaiswal",30000);
8.
9. document.write(e.id+" "+e.name+" "+e.salary);
10. </script>
Output of the above example
103 Vimal Jaiswal 30000
1. <script>
2. function emp(id,name,salary){
3. this.id=id;
4. this.name=name;
5. this.salary=salary;
6.
7. this.changeSalary=changeSalary;
8. function changeSalary(otherSalary){
9. this.salary=otherSalary;
10. }
11. }
12. e=new emp(103,"Sonoo Jaiswal",30000);
13. document.write(e.id+" "+e.name+" "+e.salary);
14. e.changeSalary(45000);
15. document.write("<br>"+e.id+" "+e.name+" "+e.salary);
16. </script>
JavaScript Objects
Real Life Objects
In real life, objects are things like: houses, cars, people, animals, or any
other subjects.
Car objects have the same properties, but the values differ from car to car.
Object Methods
Car objects have the same methods, but the methods are performed at
different times.
JavaScript Variables
Example
let car = "Fiat";
Try it Yourself »
JavaScript Objects
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to an object named car:
Example
const car = {type:"Fiat", model:"500", color:"white"};
Properties can be changed, added, deleted, and some are read only.
// objectName.property
let age = person.age;
or
//objectName["property"]
let age = person["age"];
or
//objectName[expression]
let age = person[x];
Examples
person.firstname + " is " + person.age + " years old.";
Try it Yourself »
Try it Yourself »
Try it Yourself »
You can add new properties to an existing object by simply giving it a value:
Example
person.nationality = "English";
Try it Yourself »
Deleting Properties
Example
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
delete person.age;
Try it Yourself »
or delete person["age"];
Example
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
delete person["age"];
Try it Yourself »
Note:
The delete keyword deletes both the value of the property and the property
itself.
After deletion, the property cannot be used before it is added back again.
Nested Objects
Example
myObj = {
name:"John",
age:30,
myCars: {
car1:"Ford",
car2:"BMW",
car3:"Fiat"
}
}
You can access nested objects using the dot notation or the bracket
notation:
Examples
myObj.myCars.car2;
Try it Yourself »
myObj.myCars["car2"];
Try it Yourself »
myObj["myCars"]["car2"];
let p1 = "myCars";
let p2 = "car2";
myObj[p1][p2];
JavaScript Array
JavaScript array is an object that represents a collection of similar type of
elements.
1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)
var arrayname=[value1,value2.....valueN];
As you can see, values are contained inside [ ] and separated by , (comma).
Let's see the simple example of creating and using array in JavaScript.
1. <script>
2. var emp=["Sonoo","Vimal","Ratan"];
3. for (i=0;i<emp.length;i++){
4. document.write(emp[i] + "<br/>");
5. }
6. </script>
Sonoo
Vimal
Ratan
2) JavaScript Array directly (new keyword)
1. <script>
2. var i;
3. var emp = new Array();
4. emp[0] = "Arun";
5. emp[1] = "Varun";
6. emp[2] = "John";
7.
8. for (i=0;i<emp.length;i++){
9. document.write(emp[i] + "<br>");
10. }
11. </script>
Arun
Varun
John
3) JavaScript array constructor (new keyword)
1. <script>
2. var emp=new Array("Jai","Vijay","Smith");
3. for (i=0;i<emp.length;i++){
4. document.write(emp[i] + "<br>");
5. }
6. </script>
Output of the above example
Jai
Vijay
Smith
Let's see the list of JavaScript array methods with their description.
Methods Description
copywithin() It copies the part of the given array with its own
elements and returns the modified array.
entries() It creates an iterator object and a loop that iterates
over each key/value pair.