User FAQs

If you have a question, please send it to us.


1) What is ECMAScript ?
This is the official name given to the language standard that Netscape originally submitted to ECMA for standardization.
Despite the official name this language is more commonly referred to as JavaScript.


2) Is JavaScript a Strongly Typed programming language ?
No. Type conversions can be implicit. This has nothing to do with Strict Mode.
JavaScript is dynamically typed. Data types are detected at run-time not during compilation.
You can reuse and reassign variables to lots of different types.

var myVar = '100';  
myVar = 20 * myVar;
console.log( typeof(myVar) ); // "number"

3) Can you explain the difference between Java and JavaScript ?
Java - is a complete and comprehensive programming language
JavaScript - is a client-side scripting language


4) What is Strict Mode ?
This is an option that can be used to force you to explicitly declare a variable before it can be used.
This allows you to place an entire file/script or just an individual function in a "strict" operating context.
Modules (ES 2015) are automatically placed in strict mode.

'use strict'

function fun_One() {
'use strict'
};

5) Can you give some examples of other actions that are prevented when using strict mode ?
*) The strings 'eval' and 'arguments' cannot be used as variable names.
*) Writing to a read-only property is not allowed.
*) The 'with' statement is not allowed.


6) Is JavaScript an Object Orientated programming language ?
Yes although it is not a class based object orientated language like C#
Polymorphism - Yes because it is not strictly typed
Encapsulation - Yes. Using modules (IIFE)
Inheritance (Interface) -
Inheritance (Implementation) -
No
Inheritance (Implementation) - Can be achieved using prototyping but is at the expense of encapsulation


7) Are semicolons required at the end of statements ?
Certain statements must be terminated with semicolons.
Semicolons can be omitted in certain situations and when omitted they will be inserted for you automatically.
If you do not include them you will be relying on a process called Automatic Semicolon Insertion (ASI).
There are a number of cases when the ASI will insert one in the wrong place.

a = b + c 
(d + e).print()

This translates to "a = b + c(d + e).print" which is different.

return 
{
   var1 = 'Mon'
}

This translates to "return; { var1 = 'Mon' }" which is different.


8) What is the difference between 'var', 'let' and 'const'?
A variable declared with var outside a function has global scope.
A variable declared with var inside a function has function scope. This will take precedence over the same variable declared outside the function.
The scope of var can be global or function (not block).
The scope of let can be global, function or block.
The scope of const can be global, function or block and cannot be reused or reassigned later.
A variable declared with let at the global scope will not be added to the global window object.

var myVar = 'Tuesday';  
function fun_One() {
   var myVar = 'Wednesday';
   console.log( myVar );
}
console.log( myVar ); // Tuesday
fun_One(); // Wednesday

9) What are 'undeclared' and 'undefined' variables ?
Undeclared variables - are those that do not exist and have not be declared. Reading the value from an undeclared variable will generate a runtime error.
Undefined variables - are those that have been declared but have not been given a value. Reading the value from an undefined variable will give you undefined.
If you declare a variable and do not assign a value to it, its value will be undefined.

var myUndefined; 
console.log( myUndefined ); // "undefined"
console.log( myUndeclared ); // Run-time Error: "myUndeclared" is not defined

10) Can you describe the difference between 'undefined' and 'null' ?
undefined - a variable that has not been given a value. It has not been initialised.
null - a variable that has been explicitly set to have no value.

var myVar1; 
myVar1 = null;
console.log( myVar1 );

If you compare undefined and null using either of the following lines, they will be equal.

if (null == undefined)        // return true 
if (null === undefined) // return true

null is also considered an object.

let myLet1 = null; 
console.log( typeof myLet1 === 'object' ) // true
let myLet2 = [];
console.log( toString.call(myLet2) === '[object Array]' ); // true
console.log( myLet2.constructor === Object );

11) Can you describe the different variable scopes ?
There are three scopes, global, function and block.
global scope - can be accessed from anywhere in the program
function scope - can be accessed only from within the function where it is declared
block scope - can be accessed only from within the block


12) How do you declare a variable ?
If you do not use the var keyword then it will be given global scope.
You can use the 'var' keyword to give your variable function scope.

myGlobal1 = 5;                    // variables are case sensitive 
var myVarGlobal2 = 10;
function fun_One() {
   myGlobal3 = 10 * 2;
   var myVarFunction1 = 10 * 2; // has function scope
};

13) Can you describe Variable Hoisting ?
JavaScript will automatically move all variable declarations to the top of their corresponding scope.
The initialisation will not be hoisted, only the declaration.

var myVar = 'Monday'  
function fun_One() {
   if (myVar === undefined) {
      var myVar = 'Tuesday'
   }
   console.log( myVar );
};
fun_One(); // Tuesday

The (var myVar) part of the (var myVar = 'Tuesday') is automatically moved above the if statement.
This creates a new variable "myVar" that has function scope declared inside the "fun_One" function.
This has the same name as the variable at the global scope and therefore takes precedence.


14) What is the typeof operator ?
This is a unary operator that takes a single operand and returns a 'string' description of the data type.

console.log typeof ( 'Hello World' );  // 'string'  
console.log typeof ( 19.5 ); // 'number'
console.log typeof ( true ); // 'boolean'

var myFunction = function(x, y) {
    return x + y;
};
console.log typeof ( myFunction ) // 'function'

15) Can you give some examples of Value data types ?
Also known as primitive data types.

number          // integer and floating point 
string
boolean
symbol // added in ES 2015
null // represents an empty
undefined

16) Can you give some examples of Reference data types ?

Function 
Array
Object
Object - Number
Object - String
Object - Boolean

17) How can you check if a variable has a value ?
You can use the if keyword to check if a variable has a value.

if (myVar) { 
   // 0, NaN, "", null, undefined
}
else {
  // everything else
}

18) Can a variable be declared with a specific data type ?
No. JavaScript is a dynamically typed language which means that a data type is assigned to a variable at run-time depending on its value.
The data type of a variable can change at run-time and is dictated by the variable's value.
In JavaScript this means you can just assign anything you want to the variable and it will work.

var test = 42; 
test = 'the answer';
test = function () {};

TypeScript however is a statically typed language so it is possible to declare a variable with a specific data type.


19) Can you describe the 'boolean' data type ?
This can contain the values true or false.

equals (==) 
not equals (!=)
null == undefined // true
false == 0; // true
 == 0; // true - type coerced
123' == 123 // true
boolean + number = addition

20) Can you explain the difference between the Boolean() function and the Boolean constructor function ?
The Boolean() function should be used with primitive/literal values.
When used with literal values this function can be used to convert literal values to a 'boolean' data type.

let bool1 = Boolean(false);           // typeof = boolean 

The same function name can also be used with the 'new' keyword as a Constructor function.
When used with the new keyword a Boolean object is created.

let bool2 = new Boolean(false);       // typeof = object 

It is considered bad practice to use this function with the new keyword because Boolean objects are always truthy.
To initialise a boolean variable just use one of the primitive data types (true or false).

let bool2 = false;                    // typeof = boolean 

21) Can you convert a 'boolean' data type to a 'string' data type ?

let myLetBool = false; 
let myLetStr;
myLetStr = myLetBool.toString();
myLetStr = false.toString();
myLetStr = String('false');

22) Can you convert a 'boolean' data type to a 'number' data type ?



23) Can you describe the logical operators ?

&& - and 
|| - or
! - not

JavaScript uses short-circuit evaluation


24) Can you describe the 'Symbol' data type ?
Added in ES 2015.
A symbol represents a unique identifier.
Using symbols guarantees uniqueness. The description is just a label.
Symbol conversion has to be explicit.

let myLetID1 = Symbol("id"); 
let myLetID2 = Symbol("id");
console.log( myLetID1 === myLetID2); // 'false'

Unlike Number, String and Boolean it is not possible to create an explicit Symbol object using the new keyword.


25) Is there a way of checking for equality ?
Yes. You can use the operators (==) and (!=).
The '==' operator checks for implicit type conversion.

null == undefined;   // true 
false == 0; // true
' == 0; // true
'123' == 123; // true
206 != 207; // false

26) Can you explain the following results ?

let myLet1 = [1,2,3]; 
let myLet2 = [1,2,3];
let myLet3 = "1,2,3";
myLet1 == myLet3; // true
myLet2 == myLet3; // true
myLet1 == myLet2; // false

27) Is there a way of checking for equality AND data type ?
Yes. You can use the operators (===) and (!==).
The '===' operator checks for explicit type conversion.

null === undefined            // false 
false === 0; // false
'' === 0; // false
123' === 123 // false

let myLetNum1 = 10;
let myLetNum2 = 10;
console.log( myLetNum1 == myLetNum2 ); // true
console.log( myLetNum1 === myLetNum2 ); // true

let myLetNum3 = new Number(10);
let myLetNum4 = new Number(10);
console.log( myLetNum3 == myLetNum4 ); // false
console.log( myLetNum3 === myLetNum4 ); // false

28) Can you describe the 'alert', 'prompt' and 'confirm' functions ?
This displays a message to the user in a modal window.

window.alert('this is a useful message'); 

This displays a textbox asking the user for input in a modal window.
The textbox can have a default value.

var promptResult = window.prompt('This is a prompt for information', 'default value'); 

This displays a question to the user asking them to choose either OK or Cancel.

var confirmResult = window.confirm('Do you confirm?'); 

29) Write a While loop ?
A while loop can execute zero or many times

var x = 10; 
while (x > 0) {
   x--;
   console.log('The value of x is ' + x);
};

30) Write a Do loop ?
A do loop executes at least once.
A do loop contains the keyword while.

var x = 0; 
do {
   x++;
   console.log('The value of x is ' + x);
} while (x > 4);

31) Can you describe the 'break' keyword ?
This provides a way to exit the current loop.
If you are in a nested loop this will only exit one level.

var text = ''";  
for (i = 0; i < 10; i++) {
  if (i === 3) {
      break;
   }
  text += "The number is " + i + "<br>";
};

32) Can you describe the 'continue' keyword ?
This continues with the next statement of the loop.

var text = '';  
for ( I = 0; I < 5; i++) {
   if (I === 3) {
      continue;
   }
   text += 'The number is ' + i + "<br>";
}

33) What is the 'delete' operator ?
The delete operator can be used to delete properties from an object.



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext