Javascript & CSS notes
Javascript & CSS notes
o Lightweight
o Interpreted programming language
o Good for the applications which are network-centric
o Complementary to Java
o Complementary to HTML
o Open source
Cross-platform
1. function msg()
2. {
3. document.writeln("Named Function");
4. }
5. msg();
o Named - These type of functions contains name at the time of definition. For
Example:
1. function display()
2. {
3. document.writeln("Named Function");
4. }
5. display();
o Anonymous - These type of functions doesn't contain any name. They are declared
dynamically at runtime.
1. var display=function()
2. {
3. document.writeln("Anonymous Function");
4. }
5. display();
1. var display=function()
2. {
3. alert("Anonymous Function is invoked");
4. }
5. display();
1. var str="Javatpoint";
2. document.writeln(str.charAt(4));
1. <script type="text/javascript">
2. document.write("JavaScript Hello World!");
3. </script>
The window object is used to display the popup dialog box. Let's see with description.
Method Description
alert() displays the alert box containing the message with ok button.
confirm() displays the confirm dialog box containing the message with ok and
cancel button.
setTimeout() performs the action after specified time like calling function,
evaluating expressions.
1. function function_name(){
2. //function body
3. }
22) What are the JavaScript data types?
There are two types of data types in JavaScript:
1. By object literal
2. By creating an instance of Object
3. By Object Constructor
1. emp={id:102,name:"Rahul Kumar",salary:50000}
1. By array literal
2. By creating an instance of Array
3. By using an Array constructor
1. var emp=["Shyam","Vimal","Ratan"];
1. function number(num) {
2. if (isNaN(num)) {
3. return "Not a Number";
4. }
5. return "Number";
6. }
7. console.log(number('1000F'));
8. // expected output: "Not a Number"
9.
10.console.log(number('1000'));
11.// expected output: "Number"
1. function display()
2. {
3. document.writeln(10+20+"30");
4. }
5. display();
1. function display()
2. {
3. document.writeln("10"+20+30);
4. }
5. display();
The Internet Explorer stores the cookies on a file [email protected]. The path is:
c:\Windows\Cookies\[email protected].
Null value: A value that is explicitly specified by the keyword "null" is known as a null
value. For example:
1. <script>
2. window.document.body.style.cursor = "wait";
3. </script>
1. var num=-5;
2. function display()
3. {
4. document.writeln(num/0);
5. }
6. display();
7. //expected output: -Infinity
1. var address=
2. {
3. company:"Javatpoint",
4. city:"Noida",
5. state:"UP",
6. fullAddress:function()
7. {
8. return this.company+" "+this.city+" "+this.state;
9. }
10.};
11.var fetch=address.fullAddress();
12.document.writeln(fetch);
1. function display()
2. {
3. x = 10;
4. y = 15;
5. z = x + y;
6. debugger;
7. document.write(z);
8. document.write(a);
9. }
10.display();
1. "use strict";
2. x=10;
3. console.log(x);
1. function display()
2. {
3. document.writeln(Math.random());
4. }
5. display();
1. function display()
2. {
3. var date=new Date();
4. var day=date.getDate();
5. var month=date.getMonth()+1;
6. var year=date.getFullYear();
7. document.write("<br>Date is: "+day+"/"+month+"/"+year);
8. }
9. display();
1. function display()
2. {
3. var x=102;//integer value
4. var y=102.7;//floating point value
5. var z=13e4;//exponent value, output: 130000
6. var n=new Number(16);//integer value by number object
7. document.write(x+" "+y+" "+z+" "+n);
8. }
9. display();
1. function display()
2. {
3. document.writeln(10<20);//true
4. document.writeln(10<5);//false
5. }
6. display();
1. function display()
2. {
3. var arr1= [1,2,3,4,5,6,7,8,9,10];
4. arr1.copyWithin(2) ;
5. document.write(arr1);
6. }
7. display();
1. function display()
2. {
3. var set = new Set();
4. set.add("jQuery");
5. set.add("AngularJS");
6. set.add("Bootstrap");
7. for (let elements of set) {
8. document.writeln(elements+"<br>");
9. }
10.}
11.display();
1. function display()
2. {
3. var ws = new WeakSet();
4. var obj1={};
5. var obj2={};
6. ws.add(obj1);
7. ws.add(obj2);
8. //Let's check whether the WeakSet object contains the added object
9. document.writeln(ws.has(obj1)+"<br>");
10.document.writeln(ws.has(obj2));
11.}
12.display()
1. function display()
2. {
3. var map=new Map();
4. map.set(1,"jQuery");
5. map.set(2,"AngularJS");
6. map.set(3,"Bootstrap");
7. document.writeln(map.get(1)+"<br>");
8. document.writeln(map.get(2)+"<br>");
9. document.writeln(map.get(3));
10.}
11.display();
1. function display()
2. {
3. var wm = new WeakMap();
4. var obj1 = {};
5. var obj2 = {};
6. var obj3= {};
7. wm.set(obj1, "jQuery");
8. wm.set(obj2, "AngularJS");
9. wm.set(obj3,"Bootstrap");
10.document.writeln(wm.has(obj2));
11.}
12.display();
What is CSS?
Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to
simplify the process of making web pages presentable.
What are advantages of using CSS?
Following are the advantages of using CSS −
• CSS saves time − You can write CSS once and then reuse same sheet in multiple
HTML pages. You can define a style for each HTML element and apply it to as many
Web pages as you want.
• Pages load faster − If you are using CSS, you do not need to write HTML tag
attributes every time. Just write one CSS rule of a tag and apply it to all the
occurrences of that tag. So less code means faster download times.
• Easy maintenance − To make a global change, simply change the style, and all
elements in all the web pages will be updated automatically.
• Superior styles to HTML − CSS has a much wider array of attributes than HTML, so
you can give a far better look to your HTML page in comparison to HTML attributes.
• Multiple Device Compatibility − Style sheets allow content to be optimized for more
than one type of device. By using the same HTML document, different versions of a
website can be presented for handheld devices such as PDAs and cell phones or for
printing.
• Global web standards − Now HTML attributes are being deprecated and it is being
recommended to use CSS. So its a good idea to start using CSS in all the HTML pages
to make them compatible to future browsers.
• Offline Browsing − CSS can store web applications locally with the help of an offline
catche.Using of this, we can view offline websites.The cache also ensures faster
loading and better overall performance of the website.
• Platform Independence − The Script offer consistent platform independence and
can support latest browsers as well.
What are the components of a CSS Style?
A style rule is made of three parts −
• Selector − A selector is an HTML tag at which a style will be applied. This could be
any tag like <h1> or <table> etc.
• Property − A property is a type of attribute of HTML tag. Put simply, all the HTML
attributes are converted into CSS properties. They could be color, border etc.
• Value − Values are assigned to properties. For example, color property can have value
either red or #F1F1F1 etc.
What is type selector?
Type selector quite simply matches the name of an element type. To give a color to all level
1 headings −
h1 {
color: #36CFFF;
}
*{
color: #000000;
}
This rule renders the content of every element in our document in black.
What is Descendant Selector?
Suppose you want to apply a style rule to a particular element only when it lies inside a
particular element. As given in the following example, style rule will apply to <em> element
only when it lies inside <ul> tag.
ul em {
color: #000000;
}
What is class selector?
You can define style rules based on the class attribute of the elements. All the elements
having that class will be formatted according to the defined rule.
.black {
color: #000000;
}
This rule renders the content in black for every element with class attribute set to black in
our document.
Can you make a class selector particular to an element type?
You can make it a bit more particular. For example −
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class attribute set to
black.
What is id selector?
You can define style rules based on the id attribute of the elements. All the elements having
that id will be formatted according to the defined rule.
#black {
color: #000000;
}
This rule renders the content in black for every element with id attribute set to black in our
document.
Can you make a id selector particular to an element type?
ou can make it a bit more particular. For example −
h1#black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with id attribute set to black.
What is a child selector?
Consider the following example −
body > p {
color: #000000;
}
This rule will render all the paragraphs in black if they are direct child of <body> element.
Other paragraphs put inside other elements like <div> or <td> would not have any effect
of this rule.
What is an attribute selector?
You can also apply styles to HTML elements with particular attributes. The style rule below
will match all the input elements having a type attribute with a value of text −
input[type = "text"] {
color: #000000;
}
The advantage to this method is that the <input type = "submit" /> element is unaffected,
and the color applied only to the desired text fields.