Declaring an Int Variable
var intNum = 10; // Assigning value while declaring
var anotherNum; // Declaring variable without assigning
anotherNum = -20; // Assigning value after declaring
Mathematical Operations with Int Values
var num1 = 20;
var num2 = 10;
var result1 = num1 + num2; // Addition
var result2 = num1 – num2; // Subtraction
var result3 = num1 * num2; // Multiplication
var result4 = num1 / num2; // Division
var result5 = num1 % num2; // Modulus
- Always declare variables properly: make sure to use var, let, or const to declare variables and assign integer values. Avoid using undeclared variables, which can lead to errors and security vulnerabilities.
- Avoid unnecessary conversions: be mindful of type conversions when using operators or passing values between functions. Avoid unnecessary conversions between integers and other data types, such as strings or boolean values.
- Use strict equality: when comparing integer values, use strict equality (===) to ensure that both the value and data type match. This can help prevent unexpected results when comparing values that may be coerced to other types.
- Avoid using parseInt: while parseInt can be useful for converting strings to integers, it’s important to use it carefully and avoid issues with leading zeros or non-numeric characters. Instead, consider using unary plus (+) or Number() to convert strings to integers.
- Optimize calculations: when performing math calculations with integers, be mindful of performance by using optimized algorithms and avoiding unnecessary operations.
parseInt() function. This function takes a string value as its argument and returns the integer equivalent.
Here’s an example:
// string value const strValue = "42"; // convert to integer value const intValue = parseInt(strValue); // output integer value console.log(intValue); // 42
It’s important to note that if the string value contains non-numeric characters, the
parseInt() function will only return the integer portion of the value. For example:
// string value with non-numeric characters const strValue = "42px"; // convert to integer value const intValue = parseInt(strValue); // output integer value console.log(intValue); // 42
If you need to convert a string value that contains decimal points to an integer value, you can use the
parseFloat() function to get the decimal value and then use the
Math.round() function to round it to the nearest integer. For example:
// string value with decimal point const strValue = "3.14"; // convert to integer value const intValue = Math.round(parseFloat(strValue)); // output integer value console.log(intValue); // 3
- Bitwise Operators: The bitwise operators (
<<) can manipulate the binary representation of values, which can be useful in some situations.
- Hexadecimal Notation: You can represent integer values in hexadecimal notation, which can make some manipulations easier. For example, you can use the
0xprefix to represent a hexadecimal value.
- Conversion Methods: There are many methods for converting integer values to other representations, such as strings or arrays. These methods can help you manipulate integer values in different ways.
var x = 5.25;
var y = x | 0; // y will now be 5