A few topics on JavaScript that we should know…

Shahin
4 min readNov 3, 2020

#1. How to use comments in javaScript development?

Comments are lines of code that JavaScript will intentionally ignore.
There are two ways to write comments in javaScript:
Using // will tell JavaScript to ignore the remainder of the text on the current line.
example:
// This is a single-line comment

using /*….*/ will tell JavaScript to ignore multiline texts.
example:
/*This is a
multi-line comment*/

Comments are a great way to leave notes to your self and to other people who will later need to figure out what a block of code does

#2. How to handle errors in JavaScript?

JavaScript has a syntax construct try…catch that allows us to “catch” error. The trycatch construct has two main blocks: try, and then catch.
When an error occurs in the try block, we can handle it in the catch block. Usually, a Script stops immediately in case of an error and gives an error message in the console. this way a user gets a bad experience with a website.

syntax:
try{
code…
}catch(err){
error-handling code…
}

#3. How to throw custom errors?

If we want to create custom errors in JavaScript we can use the “throw” operator. We can use anything as an error such as strings, numbers, objects, and so on. it is useful when an error occurs in script code, we waste so much time to find the cause of the error. if we use the “throw” operator and create our own error then we will find it easily on our code.

syntax:

throw <error object or name or number>

#4. Why we use let and const instead of var?

When we use the var keyword to declare a variable it behaves like a global scope variable and it can be accessed by anywhere in the code. Sometimes this behavior causes different kinds of error and the developers waste so much time to fix the errors. That’s why many developers use the let and const keyword instead of var. A variable declared with the let keyword it behaves as a block scope variable and it is not accessible from outside of the block. besides, the let keyword not allow us to repeat a variable name. A variable with the const keyword has the same benefit as the let keyword. It has rules that you can not reassign variable which previously assigned with const. if that happens it will throw an error. Some times we need to reassign a variable there we can use let instead of const

#5. What is the Spread Operator?

Spread Operator (…) have different uses. We can use it to copy some elements from an array or from objects. besides, using this operator before the parameter’s name we can pass as many arguments as we need in the function.

example:

const numbers = [2, 3, 5, 6, 7];
const num = […numbers]
console.log(num)// expected output: [2, 3, 5, 6, 7]

#6. What is an Arrow function?

JavaScript ES6 has a simple and concise syntax for creating functions that’s often better than traditional function expressions. it’s a shorter version of an anonymous function(a function created without a name). it is the same as traditional function but it has some limitations. e.g. it does not have arguments or new.target keywords and not suitable for call, apply and bind methods

An array method looks like:

const add = (a, b) => a * b;

or const add = (a, b) => {
//some code…
}

#7. How to work the typeof() operator?

JavaScript has 9 types of value, 6 of them are primitives values and 3 of them are structural types value. The primitive values are string, number, boolean, undefined, bigint, symbol, and structural type values are objects, functions If we want to check the type of values we can use typeof() operator. it returns a string indicating the type of the unevaluated operand.

example:

console.log(typeof(78)) // expected output: “Number”
console.log(typeof(‘name’)) // expected output: “String”
console.log(typeof({})) // expected output: “Object”

#8. What’s the benefit of using functions with the default parameter?

We can use Function with default parameter because sometimes functions not get any value from users that cause an error; When an error occurs the entire script gets stuck and the users never get a smooth experience on the website. So, in those cases, the default parameter can be a good choice. the programmer doesn’t need to worry about the empty or undefined arguments in the function.

example:

function multiply(a, b = 1) {
return a * b;
}
console.log(multiply(5)); // expected output: 5
console.log(multiply(5, 2)) // expected output: 10

#9. What is hoisting?

Hoisting is JavaScript’s default behavior of moving declarations to the top. but stay exactly where we typed them in our code. JavaScript declarations are hoisted mean a variable can be used before it has been declared. Sometimes we see that a function is used before it has been declared.

example:

catName(“Chloe”);

function catName(name) {
console.log(“My cat’s name is “ + name);
}

Note that: JavaScript only hoists declarations, not initializations

example:

console.log(num); // Returns undefined, as only declaration was hoisted, no initialization has happened at this stage
var num; // Declaration
num = 6; // Initialization

#10. What is Cross-browser testing?

Cross-browser testing means a website or web application that was created by you that works across an acceptable number of web browsers. we can test the website by running it in different browsers such as Chrome, Firefox, Safari, IE/Edge, etc. sometimes our created website does not work in different browsers. Because different browsers have different types of facilities and they implement different types of features to compete with each other on the global market. that’s why we need to implement some extra code to make sure to work on all browsers.

--

--

Shahin
0 Followers

passionate web developer and I am enthusiast about learning new things for web development