This content originally appeared on DEV Community and was authored by Aman kumar
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.
Understanding Data Types in JavaScript
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.
typeof
Operator
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"
-
undefined
: When a variable is declared but not assigned a value, its type isundefined
. -
null
: Represents the intentional absence of any object value. Interestingly,typeof null
returnsobject
. This is a known quirk in JavaScript, and while it might seem confusing, it’s something you’ll get used to.
Key Data Types
-
Number: Represents both integers and floating-point numbers.
- Example:
let num = 42;
- JavaScript can handle numbers up to
2^53
, beyond which you would use aBigInt
.
- Example:
-
BigInt: Used for large integers that are beyond the safe integer range for
Number
.- Example:
let bigNum = 9007199254740991n;
- Example:
-
String: A sequence of characters, used for text.
- Example:
let greeting = "Hello, World!";
- Example:
-
Boolean: Represents logical values:
true
orfalse
.- Example:
let isJavaScriptFun = true;
- Example:
-
Null: A special keyword denoting a null value, i.e., no value at all.
- Example:
let emptyValue = null;
- Example:
-
Undefined: Indicates that a variable has been declared but not assigned a value.
- Example:
let something;
- Example:
-
Symbol: Represents a unique identifier. Symbols are often used as keys in objects to avoid name collisions.
- Example:
let uniqueId = Symbol("id");
- Example:
Enforcing Modern JavaScript with "use strict"
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.
Performing Basic Operations in JavaScript
JavaScript supports a wide range of operations, from basic arithmetic to string concatenation. Let's look at some examples:
Arithmetic Operations
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)
String Operations
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.
Combined Operations
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
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)
Variable Assignment Chaining
You can chain assignments in JavaScript:
let num1, num2, num3;
num1 = num2 = num3 = 2 + 2;
console.log(num1, num2, num3); // All will output: 4
Increment Operators
JavaScript also supports increment (and decrement) operators:
let gameCounter = 100;
++gameCounter;
console.log(gameCounter); // Outputs: 101
Prefix vs. Postfix Increment
JavaScript distinguishes between prefix (++gameCounter
) and postfix (gameCounter++
) increments:
- Prefix: Increments the value before using it.
- Postfix: Uses the value first, then increments it.
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!!
This content originally appeared on DEV Community and was authored by Aman kumar
Aman kumar | Sciencx (2024-08-31T11:32:39+00:00) Day 2: Deep Dive into JavaScript Data Types, Strict Mode, and Basic Operations. Retrieved from https://www.scien.cx/2024/08/31/day-2-deep-dive-into-javascript-data-types-strict-mode-and-basic-operations/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.