Comparison and Logical operators có thể hiểu nôm na là so sánh và biểu thức điều kiện, chúng được sử dụng để kiểm tra một điều kiện nào đó đúng hay sai.
Nội dung chính Show
- JavaScript Comparison and Logical Operators trong Javascript
- Cách sử dụng
- Logical Operators
- Conditional [Ternary] Operator
- Comparing Different Types
- Cùng chuyên mục:
Nội dung chính
- JavaScript Comparison and Logical Operators trong Javascript
- Cách sử dụng
- Logical Operators
- Conditional [Ternary] Operator
- Comparing Different Types
- Cùng chuyên mục:
Nội dung chính
- JavaScript Comparison and Logical Operators trong Javascript
- Cách sử dụng
- Logical Operators
- Conditional [Ternary] Operator
- Comparing Different Types
- Cùng chuyên mục:
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
Các toán tử so sánh được sử dụng trong các câu lệnh logic để xác định xem giá trị của biến này có bằng, lớn hơn, nhỏ hơn .vv. giá trị của biến kia hay không.
Toán tửMô TảVí dụKết quảDEMO==so sánh bằng giá trịx = 5, x == 8
x = 5, x == 5
Bài viết này được đăng tại [free tuts .net]
x = 5, x == "5"
False
True
True
DEMO
DEMO
DEMO
===so sánh bằng giá trị và kiểu dữ liệux = 5, x === 5
x = 5, x === "5"
True
False
DEMO
DEMO
!==so sánh khác giá trịx = 5, x != 10
TrueDEMO!==so sánh khác giá trị hoặc khác kiểu dữ liệux = 5, x !== 5
x = 5, x !== "5"
x = 5, x !== 10
False
True
True
DEMO
DEMO
DEMO
>lớn hơnx = 5, x > 3TrueDEMO=lớn hơn hoặc bằngx = 5, x >= 6FalseDEMO 4 & y > 3]TrueDEMO||so sánh hoặcx = 5, y = 4, [ x > 4 || y > 6]TrueDEMO!phủ định của phép so sánhx = 5, y = 4, ![ x == y]TrueDEMOConditional [Ternary] Operator
Conditional Operator có thể hiểu là một biểu thức điều kiện, javascript sử dụng nó để gán giá trị cho biến, giá trị sẽ thay đổi tùy thuộc vào kết quả trả về của điều kiện.
Cú pháp: variablename = [condition] ? value1:value2
var skill = [score > 50] ? "good":"bad";
Trong ví dụ trên, nếu điểm bạn nhập vào nhỏ hơn 50 hoặc bằng 50, 'your skill is bad" sẽ được in ra, ngược lại, nếu score cao hơn 50, "your skill is good" sẽ được in ra.
Comparing Different Types
Các phép so sánh đối với các kiểu dữ liệu khác nhau có thể trả về những kết quả không theo ý muốn.
Khi so sánh một chuỗi với một số, chuỗi sẽ được chuyển thành kiểu số và tiến hành so sánh. Một chuỗi rỗng sẽ được chuyển thành 0, một chuỗi không thể chuyển thành kiểu số sẽ được chuyển thành NaN, các phép so sánh với NaN sẽ luôn trả về False.
Ví dụKết quảDEMO10 < 99TrueDEMO10 < "99"TrueDEMO10 > "ten"FalseDEMO10 < "ten"FalseDEMO10 == "ten"FalseDEMO"10" < "2"TrueDEMO"10" > "2"FalseDEMO"10" == "2"FalseDEMOKhi so sánh hai chuỗi, "2" sẽ lớn hơn "10" vì 2 lớn hơn 1 theo bảng chữ cái. Để có kết quả chính xác, các biến nên được chuyển thành kiểu Number trước khi so sánh.
Get started by including Bootstrap’s production-ready CSS and JavaScript via CDN without the need for any build steps. See it in practice with this Bootstrap CodePen demo.
Create a new
index.html
file in your project root. Include thetag as well for proper responsive behavior in mobile devices.
Bootstrap demo
Hello, world!
Include Bootstrap’s CSS and JS. Place the
tag in the for our CSS, and the
You can also include Popper and our JS separately. If you don’t plan to use dropdowns, popovers, or tooltips, save some kilobytes by not including Popper.
Hello, world! Open the page in your browser of choice to see your Bootstrapped page. Now you can start building with Bootstrap by creating your own layout, adding dozens of components, and utilizing our official examples.
As reference, here are our primary CDN links.
DescriptionURLCSS//cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css
JS
0You can also use the CDN to fetch any of our additional builds listed in the Contents page.
Next steps
JS components
Curious which components explicitly require our JavaScript and Popper? Click the show components link below. If you’re at all unsure about the general page structure, keep reading for an example page template.
Show components requiring JavaScript- Alerts for dismissing
- Buttons for toggling states and checkbox/radio functionality
- Carousel for all slide behaviors, controls, and indicators
- Collapse for toggling visibility of content
- Dropdowns for displaying and positioning [also requires Popper]
- Modals for displaying, positioning, and scroll behavior
- Navbar for extending our Collapse and Offcanvas plugins to implement responsive behaviors
- Navs with the Tab plugin for toggling content panes
- Offcanvases for displaying, positioning, and scroll behavior
- Scrollspy for scroll behavior and navigation updates
- Toasts for displaying and dismissing
- Tooltips and popovers for displaying and positioning [also requires Popper]
Important globals
Bootstrap employs a handful of important global styles and settings, all of which are almost exclusively geared towards the normalization of cross browser styles. Let’s dive in.
HTML5 doctype
Bootstrap requires the use of the HTML5 doctype. Without it, you’ll see some funky and incomplete styling.
...
Bootstrap is developed mobile first, a strategy in which we optimize code for mobile devices first and then scale up components as necessary using CSS media queries. To ensure proper rendering and touch zooming for all devices, add the responsive viewport meta tag to your .
You can see an example of this in action in the quick start.
Box-sizing
For more straightforward sizing in CSS, we switch the global
1 value from
2 to
3. This ensures
4 does not affect the final computed width of an element, but it can cause problems with some third-party software like Google Maps and Google Custom Search Engine.On the rare occasion you need to override it, use something like the following:
.selector-for-some-widget {
box-sizing: content-box;
}
With the above snippet, nested elements—including generated content via
5 and
6—will all inherit the specified
1 for that
8.Learn more about box model and sizing at CSS Tricks.
Reboot
For improved cross-browser rendering, we use Reboot to correct inconsistencies across browsers and devices while providing slightly more opinionated resets to common HTML elements.
Stay up-to-date on the development of Bootstrap and reach out to the community with these helpful resources.