Welcome back to our JavaScript tutorial series! In today's post, we’ll explore some fundamental concepts in JavaScript: data types, strict mode, and basic operations. These are the building blocks of any JavaScript program, so understanding them well will set you up for success as you dive deeper into coding.
JavaScript is a dynamically typed language, which means you don’t have to specify the type of data a variable holds. The type is determined automatically based on the value assigned. Let's take a closer look at some of the key data types in JavaScript.
The typeof operator is used to determine the type of a variable. Here’s a quick example:
console.log(typeof undefined); // Outputs: "undefined" console.log(typeof null); // Outputs: "object"
Number: Represents both integers and floating-point numbers.
BigInt: Used for large integers that are beyond the safe integer range for Number.
String: A sequence of characters, used for text.
Boolean: Represents logical values: true or false.
Null: A special keyword denoting a null value, i.e., no value at all.
Undefined: Indicates that a variable has been declared but not assigned a value.
Symbol: Represents a unique identifier. Symbols are often used as keys in objects to avoid name collisions.
To ensure your code adheres to the latest JavaScript standards and to avoid common pitfalls, you can use "use strict" at the beginning of your scripts or functions.
"use strict"; // All code below will be treated as newer version of JavaScript
Strict mode helps catch common coding mistakes, such as using undeclared variables, and makes your code more secure and optimized.
JavaScript supports a wide range of operations, from basic arithmetic to string concatenation. Let's look at some examples:
let value = 3; let negValue = -value; console.log(negValue); // Outputs: -3 console.log(2 + 2); // Addition: 4 console.log(2 - 2); // Subtraction: 0 console.log(2 * 2); // Multiplication: 4 console.log(2 ** 3); // Exponentiation: 8 console.log(2 / 3); // Division: 0.666... console.log(2 % 3); // Modulus: 2 (remainder)
let str1 = "hello"; let str2 = " hitesh"; let str3 = str1 + str2; console.log(str3); // Outputs: "hello hitesh" console.log("1" + 2); // Outputs: "12" (String concatenation) console.log(1 + "2"); // Outputs: "12" (String concatenation) console.log("1" + 2 + 2); // Outputs: "122" console.log(1 + 2 + "2"); // Outputs: "32"
The order of operations matters! When JavaScript encounters a string in an arithmetic operation, it converts other operands to strings and concatenates them.
console.log((3 + 4) * 5 % 3); // Outputs: 2
Here, parentheses dictate the order of operations, ensuring that the addition happens before multiplication and modulus.
Unary operators work with a single operand. Here are a couple of examples:
console.log(+true); // Outputs: 1 (Boolean `true` is converted to 1) console.log(+""); // Outputs: 0 (An empty string is converted to 0)
You can chain assignments in JavaScript:
let num1, num2, num3; num1 = num2 = num3 = 2 + 2; console.log(num1, num2, num3); // All will output: 4
JavaScript also supports increment (and decrement) operators:
let gameCounter = 100; ++gameCounter; console.log(gameCounter); // Outputs: 101
JavaScript distinguishes between prefix (++gameCounter) and postfix (gameCounter++) increments:
For more in-depth reading on type conversions and operations, check out the ECMAScript documentation.
Today, we covered some essential JavaScript concepts, from understanding the various data types and strict mode to performing basic operations. These are foundational concepts that you'll build on as you progress in JavaScript. Make sure to practice these examples and experiment with different operations to get a better grasp.
Stay tuned for more tutorials in this series as we continue to explore the fascinating world of JavaScript!
-
Happy coding and see you in the next one!!
The above is the detailed content of Day Deep Dive into JavaScript Data Types, Strict Mode, and Basic Operations. For more information, please follow other related articles on the PHP Chinese website!