(1) What are the variable naming conventions in JavaScript?
While naming your variables in JavaScript keep following rules in mind.
You should not use any of the JavaScript reserved keyword as variable name.
These keywords are mentioned in the next section.
For example, break or boolean variable names are not valid.
JavaScript variable names should not start with a numeral (0-9).
They must begin with a letter or the underscore character.
For example, 123test is an invalid variable name but _123test is a valid one.
JavaScript variable names are case sensitive.
For example, Name and name are two different variables.
(2) How typeof operator works?
The typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.
The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value and returns true or false based on the evaluation.
(3) What is the use of type of operator?
‘Typeof’ is an operator which is used to return a string description of the type of a variable.
(4) What typeof returns for a null value?
It returns "object".
(5) Can you access Cookie using javascript?
JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookie or cookies that apply to the current web page.
(6) How to create a Cookie using JavaScript?
The simplest way to create a cookie is to assign a string value to the document.cookie object, which looks like this −
Syntax −document.cookie = "key1 = value1; key2 = value2; expires = date";
Here expires attribute is option. If you provide this attribute with a valid date or time then cookie will expire at the given date or time and after that cookies' value will not be accessible.
(7) How to read a Cookie using JavaScript?
Reading a cookie is just as simple as writing one, because the value of the document.cookie object is the cookie. So you can use this string whenever you want to access the cookie.
The document.cookie string will keep a list of name = value pairs separated by semicolons, where name is the name of a cookie and value is its string value.
You can use strings' split() function to break the string into key and values.
(8) How to delete a Cookie using JavaScript?
Sometimes you will want to delete a cookie so that subsequent attempts to read the cookie return nothing. To do this, you just need to set the expiration date to a time in the past.
(9) How to redirect a url using JavaScript?
his is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows −
<head>
<script type="text/javascript">
<!--
window.location="http://www.newlocation.com";
//-->
</script>
</head>
(10) How to print a web page using javascript?
JavaScript helps you to implement this functionality using print function of window object. The JavaScript print function window.print() will print the current web page when executed.
(11) What is Date object in JavaScript?
The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ).
Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.
(13) What is Number object in JavaScript?
The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.
Syntax −
Creating a number object −
var val = new Number(number);
If the argument cannot be converted into a number, it returns NaN (Not-a-Number).
(14) What is purpose of on Error event handler in JavaScript?
The on error event handler was the first feature to facilitate error handling for JavaScript. The error event is fired on the window object whenever an exception occurs on the page.
The on error event handler provides three pieces of information to identify the exact nature of the error –
Error message − The same message that the browser would display for the given error.
URL − The file in which the error occurred.
Line number − The line number in the given URL that caused the error.
(15) How do we add JavaScript onto a web page?
There are several way for adding JavaScript on a web page, but there are two ways which are commonly used by developers.
If your script code is very short and only for single page, then following ways are the best:
(a) You can place <script type="text/javascript"> tag inside the <head> element.
<head>
<title>Page Title</title>
<script language="JavaScript" type="text/javascript">
var name = "Vikas Ahlawta"
alert(name);
</script>
</head>
(b) If your script code is very large, then you can make a JavaScript file and add its path in the following way:
<head>
<title>Page Title</title>
<script type="text/javascript" src="myjavascript.js"></script>
</head>
(16) How to create arrays in JavaScript?
There are two ways to create array in JavaScript like other languages:
(a) The first way to create array
Declare Array:
var names = new Array();
Add Elements in Array:-
names[0] = "Vikas";
names[1] = "Ashish";
names[2] = "Nikhil";
(b) This is the second way:
var names = new Array("Vikas", "Ashish", "Nikhil");
(17) If an array with name as "names" contain three elements, then how will you print the third element of this array?
Print third array element document.write(names[2]);
Note:- Array index starts with 0.
(18) What does is NaN function do?
It returns true if the argument is not a number.
Example:
document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2013/06/23")+ "<br>");
document.write(isNaN(123)+ "<br>");
The output will be:
True
True
False
(19) What does "1"+2+4 evaluate to?
Since 1 is a string, everything is a string, so the result is 124.
(20) What does 3+4+"7" evaluate to?
Since 3 and 4 are integers, this is number arithmetic, since 7 is a string, it is concatenation, so 77 is the result.
(21) Does JavaScript support foreach loop?
JavaScript 1.6(ECMAScript 5th Edition) support foreach loop,
(22) What is this?
var myArray = [[[]]];
Ans: Three dimensional array
(23) Does JavaScript Support automatic type conversion, If yes give example.
Yes! Javascript support automatic type conversion. You should take advantage of it, It is most common way of type conversion used by Javascript developers.
Ex.
var s = '5';
var a = s*1;
var b = +s;
typeof(s); //"string"
typeof(a); //"number"
typeof(b); //"number"
(24) What is ‘this’ keyword in JavaScript?
‘This’ keyword refers to the object from where it was called.
(25) Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any?
Timers are used to execute a piece of code at a set time or also to repeat the code in a given interval of time.
This is done by using the functions setTimeout, setInterval and clearInterval.
The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay.
The setInterval(function, delay) function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled.
The clearInterval(id) function instructs the timer to stop.
Timers are operated within a single thread, and thus events might queue up, waiting to be executed.
(26) What is === operator?
=== is called as strict equality operator which returns true when the two operands are having the same value without any type conversion.
(27) What are the different types of errors in JavaScript?
There are three types of errors:
Load time errors: Errors which come up when loading a web page like improper syntax errors are known as Load time errors and it generates the errors dynamically.
Run time errors: Errors that come due to misuse of the command inside the HTML language.
Logical Errors: These are the errors that occur due to the bad logic performed on a function which is having different operation.
(28) How can a value be appended to an array?
A value can be appended to an array in the given manner – arr[arr.length] = value;
No comments:
Post a Comment