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

Vue implements click feedback instructions for water ripple effect

Table of contents Water wave effect Let's see...

IE8 uses multi-compatibility mode to display web pages normally

IE8 will have multiple compatibility modes . IE pl...

Example of converting JS one-dimensional array into three-dimensional array

Today I saw a friend asking a question in the Q&a...

Vue Element-ui form validation rule implementation

Table of contents 1. Introduction 2. Entry mode o...

Use js to write a simple snake game

This article shares the specific code of a simple...

How to implement responsiveness in Vue source code learning

Table of contents Preface 1. Key Elements of a Re...

Summary of problems that may occur when using JDBC to connect to Mysql database

First, clarify a few concepts: JDBC: Java databas...

Introduction and analysis of three Binlog formats in MySQL

one. Mysql Binlog format introduction Mysql binlo...

Detailed explanation of Svn one-click installation shell script under linxu

#!/bin/bash #Download SVN yum -y install subversi...

Nexus private server construction principle and tutorial analysis

one. Why build a Nexus private server? All develo...

Detailed explanation of asynchronous programming knowledge points in nodejs

Introduction Because JavaScript is single-threade...

How to understand Vue's simple state management store mode

Table of contents Overview 1. Define store.js 2. ...

Summary of all HTML interview questions

1. The role of doctype, the difference between st...

Docker modifies the configuration information of an unstarted container

When I first used docker, I didn't use docker...

How to process blob data in MySQL

The specific code is as follows: package epoint.m...