Ten important questions for learning the basics of Javascript

Ten important questions for learning the basics of Javascript

1. What is Javascript?

Javascript is a programming language used for web development. JavaScript runs on the client side of the web.

According to MDN, JavaScript (often abbreviated JS) is a lightweight, interpreted, object-oriented language with first-class functions and is best known as a scripting language for web pages, but it is also used in many non-browser environments. It is a prototype-based multi-paradigm scripting language that is dynamic and supports object-oriented, imperative, and functional programming styles.

2. What is DOM

DOM stands for Document Object Model. When a web page is loaded, the browser creates a DOM using the HTML and CSS files. The DOM is represented by nodes and elements. You can manipulate the DOM using javascript. It is a tree structure.

3. How JS code is executed

The question to answer is a bit big. But we can briefly talk about it. Javascript runs on the browser. Almost every browser has a JavaScript engine. V8 is the most popular among them. Chrome uses the V8 engine. Firefox, on the other hand, uses the Spider-Monkey engine.

4. Difference between == and ===

If I put it this simply, == only checks if two values ​​are the same. It does not check the types of these values. Look at the following code:

if(2=="2"){
 console.log("true")
} else {
console.log("false")
}

The above code will log true. Because it considers 2 and "2" equal since it doesn't check the type.

In contrast, === checks both type and quality. For example:

if(2==="2"){
 console.log("true")
} else {
console.log("false")
}

This will log as false. Because 2 and "2" are equal in value, but they are of different types.

5. Null and Undefined

In general, null represents an empty and non-existent value, while undefined represents a value that has been declared but not yet defined. Although you can also explicitly set undefined to a variable.

var n;
console.log(typeof(n)); // undefined

var n = null;
console.log(typeof(n)); // object

The interesting thing is that the object type in JS is null.

6. Var vs Let vs Const

Before ES6, var was the only way to declare variables. But now we have more choices.

There is a term as scope. Scope refers to where these variables can be used. var declarations are either globally scoped or function/local scoped.

It is possible to hang Var, which we will discuss in a few seconds. However, now let is preferred for variable declarations. You can use const when you don't need to change the variable later in the code. To get the difference between the two, you can read the following article, I think it is very useful.

7. Hoisting

In javascript, you can use a variable before you declare it. The concept of variable and function declarations being physically moved to the top of the code is called variable hoisting.

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

So, let and const variables won't be hoisted? The answer is much more complicated. All declarations (function, var, let, const, and class) are hoisted in JavaScript, while var declarations are initialized with undefined, but let and const declarations remain uninitialized.

8. Global variables and local variables

In javascript, scope is divided into two ways. Global and local.

Variables declared within a function are called local scope. This variable cannot be accessed outside the function. In contrast, variables declared outside a function are called global scope. It can be accessed inside the function.

var genre = "superhero" //global scope
// code here can't use superhero but genre
function myFunction() {
  var superhero = "Batman"; // local scope
  // code here CAN use superhero and genre
}

9. Closure

Closures allow us to access the scope of an outer function from an inner function. It can be created by returning a function from another function. It creates a closed environment for each instance. For example:

function sum(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = sum(5);
var add10 = sum(10);

console.log(add5(6)); // 11
console.log(add10(6)); // 16

Here, add5 and add10 are both closures. They share the same definition but store different environments.

10. Callback Function

According to MDN, a callback function is a function that is passed as an argument to another function, which is then called inside the outer function to complete some kind of routine or action. For example

function greeting(name) {
  console.log('Hello ' + name);
}

function greetEmployee(name,callback) {
  callback(name);
}

greetEmployee("Dwight",greeting);

Here, greeting function has been used inside greetEmployee function. This is what we call a callback function.

Thanks for reading this article. Hope this helps.

This concludes this article about ten important issues in learning the basics of Javascript. For more relevant basic Javascript content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript Document Object Model DOM
  • Do you know the difference between == and === in JS?
  • JavaScript undefined and null difference example analysis
  • Detailed explanation of the differences between var, let and const in JavaScript es6
  • The difference between JS pre-parsing and variable promotion in web interview
  • Detailed explanation of Hoisting in JavaScript (variable hoisting and function declaration hoisting)
  • How to turn local variables into global variables in JavaScript
  • JavaScript Closures Explained
  • In-depth understanding of JavaScript callback functions

<<:  Docker swarm simple tutorial

>>:  Detailed explanation of the implementation method and usage of CSS3 border-radius rounded corners

Recommend

Specific use of CSS front-end page rendering optimization attribute will-change

Preface When scroll events such as scroll and res...

Solution to high CPU usage of Tomcat process

Table of contents Case Context switching overhead...

How to completely uninstall Docker Toolbox

Docker Toolbox is a solution for installing Docke...

How to implement batch deletion of large amounts of data in MySQL large tables

The question is referenced from: https://www.zhih...

A practical record of checking and processing duplicate MySQL records on site

Table of contents Preface analyze Data Total Repe...

Installation and deployment of MySQL Router

Table of contents 01 Introduction to MySQL Router...

Summary of problems encountered in the implementation of Vue plug-ins

Table of contents Scene Introduction Plugin Imple...

Detailed explanation of HTML basic tags and structures

1. HTML Overview 1.HTML: Hypertext Markup Languag...

In-depth analysis of HTML table tags and related line break issues

What is a table? Table is an Html table, a carrie...

Introduction and analysis of three Binlog formats in MySQL

one. Mysql Binlog format introduction Mysql binlo...

MySQL database import and export data error solution example explanation

Exporting Data Report an error SHOW VARIABLES LIK...

Implementation of docker view container log command

Why should we read the log? For example, if the c...