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

JavaScript to achieve product magnifying glass effect

This article shares the specific code of JavaScri...

How to add java startup command to tomcat service

My first server program I'm currently learnin...

15 JavaScript functions worth collecting

Table of contents 1. Reverse the numbers 2. Get t...

The easiest way to install MySQL 5.7.20 using yum in CentOS 7

The default database of CentOS7 is mariadb, but m...

A brief discussion on how to customize the host file in Docker

Table of contents 1. Command 2. docker-compose.ym...

Using react-beautiful-dnd to implement drag and drop between lists

Table of contents Why choose react-beautiful-dnd ...

How to view and optimize MySql indexes

MySQL supports hash and btree indexes. InnoDB and...

Detailed steps to install MySQL 8.0.27 in Linux 7.6 binary

Table of contents 1. Environmental Preparation 1....

js implements mouse in and out card switching content

This article shares the specific code of js to re...

JavaScript Closures Explained

Table of contents 1. What is a closure? 1.2 Memoi...

Three ways to communicate between Docker containers

We all know that Docker containers are isolated f...

How to import js configuration file on Vue server

Table of contents background accomplish Supplemen...

Analyze the method of prometheus+grafana monitoring nginx

Table of contents 1. Download 2. Install nginx an...