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) 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'
};

4) 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.


5) What is Object Orientated Programming ?
This is a style of programming that is based on objects containing methods, properties and fields arranged in a hierarchy.
This type of programming is based around four key features:
Polymorphism - Create routines that can operate on objects of different types. This is handled by late binding and multiple interfaces.
Encapsulation - Hide parts of the implementation and hide complexity. This can be achieved using a Module Pattern.
Inheritance (Interface) - (Public Inheritance) Define methods (without implementation) that must be declared in a derived class.
Inheritance (Implementation) - (Private Inheritance) Inherit method implementation from a base class.


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 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

19) Can you explain the difference between the Boolean() function and the Boolean constructor ?
The Boolean() function can be used with primitive/literal values or with objects.
When used with literal values this function can be used to convert literal values to a 'boolean' data type.
When used with the 'new' keyword this function can be used as a constructor to create a Boolean object.

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

20) 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');

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



22) Can you describe the logical operators ?

&& - and 
|| - or
! - not

JavaScript uses short-circuit evaluation


23) 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.


24) 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

25) 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

26) 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

27) 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?'); 

28) 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);
};

29) 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);

30) 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>";
};

31) 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>";
}

32) 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