You are on page 1of 5

Don’t be a perfectionist: be clever about what is most suited to your needs.

Spending 10 days on the quirks of some topic will not be very useful if you don’t
intend to use them in the coming weeks. Understand what they are and move on: when
the necessity to use them comes you will have the opportunity to go deeper and
learn all the details about them.
===================================================================================
==================================================

Q) Why <script> tag ?


A) html tag to include javascript in webpage or to point to a JS file.

Q) what is the preferred place to put <script> tag?


A) At the very end of the body tag, in this way, the web page can load and display
within the browser while the javascript is being parsed and executed

Q) what will happen if we load the JS file through the <script> tag and embed the
JS code in the <script> tag as well. For ex - consider below:
<html>
<head>
</head>
<body>
<script type="text/javascript" src ="js/app.js">
console.log("Inside script");
</script>
</body>
</html>

File: js/app.js
console.log("Inside JS file");

A) It will ignore all the code within the <script> tag

Q) will this cause a bug <script src ="js/app.js" /> ?


A) Yes, depending upon browser to browser

Q) Can we have more than one JS files in a webpage?


<script type="text/javascript" src ="js/app.js">
<script type="text/javascript" src ="js/order.js">

A) Yes, and they will be processed in order

Q) When is a <noscript> tag used ?


A) <noscript>
<h1>
This application requires JavaScript
</h1>
</noscript>

When the Javascript is turned off in the web page, <noscript> tage will be executed
and this will be shown in the webpage.

Q) What shows in the console?


var orderId= 5005;
console.log(OrderId);
A) Uncaught ReferenceError: OrderId is not defined
JS is case-sensitive

Q) what shows in the console?


var @product = 'PRD-2000';
console.log(@product);
A) syntax error
Wrong variable naming convention

Q) what shows in the console?


var 2product = 'PRD-2000';
console.log(2product);
A) syntax error
Wrong variable naming convention

Q) what shows in the console?


var $product = 'PRD-4000'
$product += 'X2'
console.log($product)
A) "PRD-4000X2"
JS will automatically put semi-colons at the end of line for us.

Q) what will be shown in the console ?


var orderId = "ORD-9001";
orderId = 9001;
console.log(orderId);
A) 9001
In JavaScript the variables are dynamically typed

Q) what will be shown in the console ?


var orderId;
console.log(orderId);
A) undefined
When a variable is uninitialized in JavaScript, it will always be set to undefined

Q) what will be shown in the console ?


orderId = 9001;
console.log(orderId);
A) 9001
Even though we've left the var keyword, javascript will create a new global
variable

Q) what will be shown in the console ?


'use Strict';
orderId = 9001;
console.log(orderId);
A) Runtime error: variable undefined in strict mode

Q) what will be shown in the console?


for (let i = 1; i != 4; i += 2) {
console.log("Still going!");
}
A) Still going! infinite times..

Q) what will be shown in the console ?


var cancelledOrders = [9001, 9002, 9003];
console.log(typeof cancelledOrders);
A) object

Q) what will be shown in the console ?


var order = null;
console.log(typeof order);
A) object
JavaScript will treat this as an object
Q) what will be shown in the console ?
function cancelOrder(orderId){
};
console.log(typeof cancelOrder);
A) function
functions operate a lot like object but they do have their own type called function

Q) Can we call a function before declaring it?


printOrder('9002');
function printOrder(orderId){
console.log("Printing order : " + orderId);
};
A) Printing order : 9002
Yes, we can do that. JavaScript in first pass will check for global variables and
function declarations. Then in the next pass it will execute the code.

Q) Would we be able to call function like this?


calculateTotalPrice(2, 4.0);
function calculateTotalPrice(quantity, price, currency){
console.log(currency);
};
A) undefined
it will not give error 'Arguments don't match up' unlike other programming
languages like Java.

Q) what will be shown in the console ?


function getOrder(){
};
var order = getOrder();
console.log(order);
A) undefined
Since the function is not returning anything, JS will assign undefined to it.

Q) Can variables hold function in JS?


var order = function(){
console.log('My order');
};
console.log(typeof order);
A) Yes
It will display 'function' in the console.

Q) what shows in the console?


var order = function(){
console.log('My order');
};
order();
A) My order

Q) what shows in the console?


var order = function(){
console.log('My order');
};
order;
A) (nothing is shown)
JS simply treats order as a reference to the variable, we're not executing the
variable as parenthesis is missing.

Q) what shows in the console?


function submitOrder() {
console.log('Submitting Order...');
}
submitorder();
A) Uncaught ReferenceError: submitorder is not defined
JS is case-sensitive

Q) What is shown in the console?


vartotal = 99.99;
varfreeShipping;
varsavings;
if(total >= 100.00) {
freeShipping= true;
savings = 29.99;
};
else{
freeShipping= false;
savings = 0;
}
console.log(savings);
A) syntax error

Q) What is shown in the console?


var orderType= 'business';
var shipMethod;
switch (orderType) {
case'business':
shipMethod= 'FedEx';
// break;
case 'personal':
shipMethod= 'UPS Ground';
break;
default:
shipMethod= 'USPS';
}
console.log(shipMethod);
A) "UPS Ground"
Note: If you omit the break statement, the next case will be executed even if the
evaluation does not match the case.

Q) What is shown in the console?


var orderTotal= 99.99;
var discount;
switch(true) {
case orderTotal>= 50&& orderTotal< 75:
discount = 10;
break;
case orderTotal>= 75&& orderTotal< 100:
discount = 20;
break;
case orderTotal>= 100:
discount = 30;
break;
default:
discount = 0;
}
console.log(discount);
A) 20
This behaves as an if-else clause
Q) What is shown in the console?
var productType = ' Hardware ';
productType.trim();
console.log('['+ productType + ']');
A) [ Hardware ]
Strings are immutable in JS

Q)
var description = 'hardware';
function updateProd(){
description = 'software';
}
updateProd();
console.log(description);
A) software

Q)
var description = 'hardware';
function updateProd(){
var description = 'software';
}
updateProd();
console.log(description);
A) software

Q) what shows in the browser?


var size = 60;
var len = size;
size = 50;
document.write(len);
A) 60
the variables will refer different memory locations although same content in memory

Primitives vs reference storage


-------------------------------
Q) what shows in the console?
var person = "Kobe";
var anotherPerson = person;
person = "Bryant";
console.log(anotherPerson);
console.log(person);
A) Kobe
Bryant

Q) what shows in the console?


var person = {name: "Kobe"};
var anotherPerson = person;
person.name = "Bryant";
console.log(anotherPerson.name);
console.log(person.name);
A) Bryant
Bryant
the value in person was stored as a reference and not an actual value, when we
changed the person.name property to “Bryant” the anotherPerson reflected the change
because it never stored an actual copy of it’s own value of the person’s
properties, it only had a reference to it.

You might also like