Data types in javascript

πŸ“‹ Personal profile

  • πŸ’– About the author: Hello, I'm Daniel 😜
  • πŸ“ Personal homepage: Hall owner a NiuπŸ”₯
  • πŸŽ‰ Support me: like πŸ‘+ Collection ⭐ Email + message πŸ“
  • πŸ“£ Series column: web development🍁
  • πŸ’¬ Maxim: so far, all life is written with failure, but it doesn't prevent me from moving forward! πŸ”₯


πŸ’ preface

Today, I'd like to summarize the data types in javascript. I hope you can gain something.

🍊 other

πŸ‡ Input and output statements in js

methodexplainascription
alert(msg)Browser pop-up alert boxbrowser
console.log(msg)Browser console printout informationbrowser
prompt(info)The browser pops up an input box, which can be entered by the userbrowser

Note: the input obtained by prompt(info) is of string type, which is similar to input() in python.

 <script>
      prompt("Please enter:");
      alert('How do you do! Cutie!');
      console.log('Daniel is the most handsome!');
  </script>



πŸ‡ Comments in js

Single-Line Comments multiline comment
///* */

Note: the comments in css are / * * /. It doesn't matter if you can't remember the comments in various languages. Just remember the shortcut key ctrl + /. Most editors support this shortcut key, which is applicable to most languages. Just select the content to be commented and ctrl + /.

πŸ‡ Name variable in js

1. js to declare a variable with the keyword var

//Declare variable
 var age ; // Declare a variable named age
 age = 18;
 var name = 'aniu' //Initialization of variables
  • var is a JS keyword used to declare variables (the meaning of variable variable). After using this keyword to declare a variable, the computer will automatically allocate memory space for the variable, which does not need to be managed by the programmer.
  • age is the variable name defined by the programmer. We need to access the space allocated in memory through the variable name.

2. Special case of declared variables

situationexplainresult
var age ; console.log ( age );Only declaration without assignmentundefined
console.log ( age )No declaration, no assignment, direct usereport errors
age =10; console.log ( age );No declaration, only assignment10

3. Variable naming convention

  • Consists of letters (A - Za - z), numbers (0-9), underscores () Dollar sign ($), such as usrage, num01_ name.
  • Strictly case sensitive. var app οΌ› And VaR app; Are two variables.
  • Cannot start with a number. 18age is wrong.
  • Cannot be keyword or reserved word. For example: var, for, while
  • Variable names must be meaningful.
  • Follow the hump nomenclature. The first letter is lowercase, and the first letter of the following word needs to be capitalized. myName

🍊 Basic data types in js

Variables are where values are stored. They have names and data types. The data type of the variable determines how the bits representing these values are stored in the computer's memory. JavaScript is a weakly typed or dynamic language. This means that there is no need to declare the type of variable in advance, and the type will be determined automatically during the operation of the program.

 var age =18; I //This is a numeric type
 var ok ='yes; //This is a string

When the code is running, the data type of the variable is determined by the JS engine according to the data type of the variable value on the right. After running, the variable determines the data type.

Simple data typeexplainDefault value
NumberNumeric type, including integer and floating-point values, such as 12 and 0.820
BooleanBoolean value types, such as true and false, are equivalent to 1 and 0false
StringString type, such as "Zhang San". Note that the strings in js are quoted" "
Undefinedvar a οΌ› Variable a is declared but no value is given. At this time, a = undefinedundefined
Nullvar a = nul οΌ› Declared that variable a is nullnull

πŸ‡ Digital Number

  • Digital hexadecimal
    The most common base systems are binary, octal, decimal and hexadecimal.
 // Octal digit sequence range: 0 ~ 7
 var num1=07; //Corresponding to decimal 7 
 var num2=018; //18 corresponding to decimal 
 var num3=08;  //8 corresponding to decimal 
 // Hexadecimal digit sequence range: 0 ~ 9 and A ~ F 
 var num =0xA; //10 corresponding to decimal


We just need to remember that in js, 0 is added before octal and 0x is added before hexadecimal.

  • Digital range
    1. The maximum and minimum values of values in JavaScript.
 console.log(Number.MAX_VALUE);//1.7976931348623157e+308 
 console.log(Number.MIN_VALUE);//5e-324

Maximum value: number MAX_ Value, the value is 1.7976931348623157e+308
Minimum value: number MIN_ Value: 5e-32

2. Infinitesimal, infinity, non numeric.

  console.log (Number.MAX_VALUE *2);// Infinity infinity
  console.log(-Number.MAX_VALUE *2);//-Infinity infinitesimal
  console.log('aniu' - 100); // NaN Not a Number
  // isNaN() this method is used to judge non numbers and return a value. If it is a number, it returns false. If it is not a number, it returns true 
  console.log(isNaN(12));// false 
  console.log(isNaN('aniu')); // true

πŸ‡ Boolean type

For example, true and false are equivalent to 1 and 0

 var flag = true;
 var flag1 = false;
 console.log(flag);
 console.log(flag1);

πŸ‡ String type string

  • Escape string in js
  • Other knowledge points of string.
    Multiple strings can be spliced with +. The splicing method is string + any type = new string after splicing. Before splicing, any type added with the string will be converted into a string, and then spliced into a new string.
 // 1. Find the length of the string
    var str = 'a niu';
    console.log(str.length);  //The length method finds the length of the string
   // 2. String splicing+
   console.log('aniu'+'a'); // aniua
   console.log('11'+13);  // 1113
   var flag = true;
   var flag1 = false;
   console.log(flag+1); // 2
   console.log(flag1+1); // 1
   var s = undefined;
   console.log(s + 'aniu'); // underfinedaniu
   console.log(s + 1); //undefined is added to the number and the result is NaN

πŸ‡ Undefined and Null

  • A variable that is not assigned after the declaration will have a default value of undefined (pay attention to the result if connecting or phase)
 var m ;
    console.log (m); //undefined 
    console.log ('A Niu'+ m);//Daniel undefined 
    console.log (11+m); // NaN 
    console.log (true + m); // NaN 

  • A declared variable is given a null value, and the value stored in it is null (when learning objects, we continue to study null)
var m  = null;
console.log (m); // null
console.log ('A Niu'+ m);//A Niu null 
console.log (11+m); // 11
console.log (true + m); // 1

πŸ’ epilogue

Today's knowledge is summarized here. The next chapter brings the data type conversion in js.
Here is the first part: πŸ‘‰ Getting to know javascript (unveiling the mystery of javascript)

Keywords: Javascript Front-end Web Development

Added by slindstr on Sat, 29 Jan 2022 18:26:44 +0200