Home Web Front-end JS Tutorial A daily javascript learning summary (basic knowledge)_javascript skills

A daily javascript learning summary (basic knowledge)_javascript skills

May 16, 2016 pm 03:32 PM

1. Character conversion

 var s1 = "01";
  var s2 = "1.1";
  var s3 = "z";//字母'z'无法转换为数字,所以或返回NaN
  var b = false;
  var f = 1.1;
  var o = { 
   valueOf: function() {
    return -1;
   }
  };
  
  s1 = -s1; //value becomes numeric -1
  s2 = -s2; //value becomes numeric -1.1
  s3 = -s3; //value becomes NaN
  b = -b;  //value becomes numeric 0
  f = -f;  //change to -1.1
  o = -o;  // 1 先执行对象的valueOf()方法返回-1,--1为1
    o = +o;//-1 o的valueOf()值为-1,+-1还是-1
Copy after login

2. Special character operations

 var result1 = 5 - true; //4 because true is converted to 1
  var result2 = NaN - 1;  //NaN NaN不是一个数字,和任何数字做任何运算都是NaN
  var result3 = 5 - 3;  //2
  var result4 = 5 - "";  //5 because "" is converted to 0
  var result5 = 5 - "2";  //3 because "2" is converted to 2
  var result6 = 5 - null; //5 because null is converted to 0
Copy after login

3. Variable to string operation

 var value1 = 10;
  var value2 = true;
  var value3 = null;
  var value4;//value4 没有赋值就是underfined 转换为字符串就是'underfined'
  
  alert(String(value1));  //"10"
  alert(String(value2));  //"true"
  alert(String(value3));  //"null"
  alert(String(value4));  //"undefined"
Copy after login

4. Number conversion

var num = 10;
  alert(num.toString());  //"10"默认十进制
  alert(num.toString(2));  //"1010"二进制
  alert(num.toString(8));  //"12"八进制
  alert(num.toString(10));  //"10"十进制
  alert(num.toString(16));  //"a"十六进制
Copy after login

5. String comparison operation

var result1 = 5 > 3; //true
  var result2 = 5 < 3; //false
  var result3 = "Brick" < "alphabet"; //true 字符串比较按照字母表顺序进行比较 小写字母在大写字母后面
  var result4 = "Brick".toLowerCase() < "alphabet".toLowerCase(); //false字母表顺序比较
  var result5 = "23" < "3"; //true 字符2小于字符3
  var result6 = "23" < 3; //false 此时'23'转换成23了
  var result7 = "a" < 3; //false because "a" becomes NaN 字符'a'无法转换成数字
  var result8 = NaN < 3; //false NaN 和任何数字比较,都无法转换成数字,所以一直是false
  var result9 = NaN >= 3; //false
Copy after login

6. Character conversion

  var num1 = parseInt("AF", 16);  //175 按照16进制输出十进制数据 10*16+15*1
  var num2 = parseInt("AF");   //NaN 没有指定进制,默认按照10进制转换,由于AF不在十进制范围,返回NaN

  alert(num1);
  alert(num2);

Copy after login

7. Use of parseInt

  var num1 = parseInt("1234blue"); //1234
  var num2 = parseInt("");   //NaN 字符''无法转换成数字
   var num3 = parseInt("0xA");   //10 - hexadecimal 16进制的A
  var num4 = parseInt(22.5);   //22
  var num5 = parseInt("70");   //70 - decimal
  var num6 = parseInt("0xf");   //15 16进制为15
Copy after login

8. Use of Number objects

  var num1 = Number("Hello world!"); //NaN
  var num2 = Number("");    //0 空字符串可以转换成0 这个parseInt()不一样
   var num3 = Number("000011");  //11
  var num4 = Number(true);   //1
Copy after login

9. NaN usage 

  alert(NaN == NaN);  //false
  alert(isNaN(NaN));  //true
  alert(isNaN(10));  //false &#63;10 is a number
  alert(isNaN("10"));  //false &#63;can be converted to number 10
  alert(isNaN("blue")); //true &#63;cannot be converted to a number
  alert(isNaN(true));  //false &#63;can be converted to number 1
Copy after login

10. System maximum number

var result = Number.MAX_VALUE + 1;
alert(isFinite(result)); // false
Copy after login

11. Infinity

  alert(5 * 6);   //30
  alert(5 * NaN);   //NaN
  alert(Infinity * 0); //NaN
  alert(Infinity * 2); //Infinity
  alert("5" * 5);   //25
  alert(true * 10);  //10
  alert(false * 10);  //0
      alert(26 % 5); //1
      alert(Infinity % 3); //NaN
      alert(3 % 0); //NaN
      alert(5 % Infinity); //5
      alert(0 % 10); //0
      alert(true % 25); //1
      alert(3 % false); //NaN
Copy after login

12. for in loop

for (var propName in window) {
    document.write(propName);
    document.write("<br />");
  }
Copy after login

13. Comparison of special characters

 alert(null == undefined); //true
  alert(null === undefined); //false
  
  alert("NaN" == NaN);  //false
  alert("NaN" === NaN);  //false
  alert(NaN == NaN);   //false
  alert(NaN === NaN);   //false
  alert(NaN != NaN);   //true
  alert(NaN !== NaN);   //true
  
  alert(false == 0);   //true
  alert(false === 0);   //false
  alert(true == 1);   //true
  alert(true === 1);   //false
  
  alert(null == 0);   //false
  alert(undefined == 0);  //false
  
  alert(5 == "5");   //true
  alert(5 === "5");   //false  
Copy after login

14. Boolean object

 var message = "Hello world!";
  var messageAsBoolean = Boolean(message);
  
  alert(messageAsBoolean); //true
Copy after login

15. Use for loop with tags
Both the break statement and the continue statement can be used in conjunction with labeled statements to return to a specific location in the code.
Usually this is done when there is a loop inside a loop, for example:

 var num = 0;
  //返回到特定的位置
  outermost:
  for (var i=0; i < 10; i++) {
    for (var j=0; j < 10; j++) {
    if (i == 5 && j == 5) {
     break outermost;
    }
    num++;
   }
  }
  
  alert(num); //55
Copy after login

In the above example, the label outermost represents the first for statement. Normally, each for statement executes the code block 10 times, which means that num will be executed 100 times under normal circumstances, and when the execution is completed, num should equal 100. The break statement here has one parameter, which is the label of the statement to jump to after stopping the loop. In this way, the break statement can not only jump out of the inner for statement (that is, the statement that uses variable j), but also jump out of the outer for statement (that is, the statement that uses variable i). Therefore, the final value of num is 55, because the loop will terminate when the values ​​of i and j are both equal to 5.
The continue statement can be used in the same way:

var iNum = 0;

outermost:
for (var i=0; i<10; i++) {
 for (var j=0; j<10; j++) {
 if (i == 5 && j == 5) {
 continue outermost;
 }
 iNum++;
 }
}

alert(iNum); //输出 "95"

Copy after login

In the above example, the continue statement will force the loop to continue, not only the inner loop, but also the outer loop. This occurs when j equals 5, meaning that the inner loop will be reduced by 5 iterations, resulting in iNum having a value of 95.

Tip: As you can see, labeled statements used in conjunction with break and continue are very powerful, but overusing them can cause trouble when debugging your code. Make sure the labels you use are descriptive and don't nest too many levels of loops.

The above is today’s summary of JavaScript learning, and it will continue to be updated every day. I hope you will continue to pay attention.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

See all articles